1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
// Copyright Materialize, Inc. and contributors. All rights reserved.
//
// Use of this software is governed by the Business Source License
// included in the LICENSE file.
//
// As of the Change Date specified in that file, in accordance with
// the Business Source License, use of this software will be governed
// by the Apache License, Version 2.0.

//! Tracks persist shards ready to be finalized, i.e. remove the ability to read
//! or write them. This identifies shards we no longer use, but did not have the
//! opportunity to finalize before e.g. crashing.

use std::collections::BTreeSet;

use differential_dataflow::lattice::Lattice;
use mz_ore::now::EpochMillis;
use mz_persist_client::ShardId;
use mz_persist_types::Codec64;
use mz_proto::RustType;
use mz_repr::TimestampManipulation;
use mz_stash::{self, TypedCollection};
use timely::order::TotalOrder;
use timely::progress::Timestamp;

use crate::client::{StorageCommand, StorageResponse};
use crate::controller::{
    Controller, DurableCollectionMetadata, ProtoStorageCommand, ProtoStorageResponse,
    StorageController,
};

pub(super) type ProtoShardId = String;
pub(super) static SHARD_FINALIZATION: TypedCollection<ProtoShardId, ()> =
    TypedCollection::new("storage-shards-to-finalize");

impl<T> Controller<T>
where
    T: Timestamp + Lattice + TotalOrder + Codec64 + From<EpochMillis> + TimestampManipulation,
    StorageCommand<T>: RustType<ProtoStorageCommand>,
    StorageResponse<T>: RustType<ProtoStorageResponse>,

    Self: super::StorageController<Timestamp = T>,
{
    /// Register shards for finalization. This must be called if you intend to
    /// finalize shards, before you perform any work to e.g. replace one shard
    /// with another.
    ///
    /// The reasoning behind this is that we need to identify the intent to
    /// finalize a shard so we can perform the finalization on reboot if we
    /// crash and do not find the shard in use in any collection.
    #[allow(dead_code)]
    pub(super) async fn register_shards_for_finalization<I>(&mut self, entries: I)
    where
        I: IntoIterator<Item = ShardId>,
    {
        SHARD_FINALIZATION
            .insert_without_overwrite(
                &mut self.state.stash,
                entries.into_iter().map(|key| (key.into_proto(), ())),
            )
            .await
            .expect("must be able to write to stash")
    }

    /// Removes the shard from the finalization register.
    ///
    /// This is appropriate to do if you can guarantee that the shard has been
    /// finalized or find the shard is still in use by some collection.
    pub(super) async fn clear_from_shard_finalization_register(
        &mut self,
        shards: BTreeSet<ShardId>,
    ) {
        let proto_shards = shards
            .into_iter()
            .map(|s| RustType::into_proto(&s))
            .collect();
        SHARD_FINALIZATION
            .delete_keys(&mut self.state.stash, proto_shards)
            .await
            .expect("must be able to write to stash")
    }

    pub(super) async fn reconcile_state_inner(&mut self) {
        // Convenience method for reading from a collection in parallel.
        async fn tx_peek<'tx, KP, VP, K, V>(
            tx: &'tx mz_stash::Transaction<'tx>,
            typed: &TypedCollection<KP, VP>,
        ) -> Vec<(K, V)>
        where
            KP: mz_stash::Data,
            VP: mz_stash::Data,
            K: RustType<KP>,
            V: RustType<VP>,
        {
            let collection = tx
                .collection::<KP, VP>(typed.name())
                .await
                .expect("named collection must exist");
            tx.peek_one(collection)
                .await
                .expect("peek succeeds")
                .into_iter()
                .map(|(k, v)| {
                    let k = K::from_proto(k).expect("deserialization to succeed");
                    let v = V::from_proto(v).expect("deserialization to succeed");
                    (k, v)
                })
                .collect()
        }

        // Get stash metadata.
        let (metadata, shard_finalization) = self
            .state
            .stash
            .with_transaction(move |tx| {
                Box::pin(async move {
                    // Query all collections in parallel.
                    Ok(futures::join!(
                        tx_peek(&tx, &super::METADATA_COLLECTION),
                        tx_peek(&tx, &SHARD_FINALIZATION),
                    ))
                })
            })
            .await
            .expect("stash operation succeeds");

        // Partition metadata into the collections we want to have and those we failed to drop.
        let (in_use_collections, leaked_collections): (Vec<_>, Vec<_>) = metadata
            .into_iter()
            .partition(|(id, _)| self.collection(*id).is_ok());

        // Get all shard IDs
        let shard_finalization: BTreeSet<_> = shard_finalization
            .into_iter()
            .map(|(id, _): (_, ())| id)
            .collect();

        // Collect all shards from in-use collections
        let in_use_shards: BTreeSet<_> = in_use_collections
            .iter()
            // n.b we do not include remap shards here because they are the data shards of their own
            // collections.
            .map(|(_, DurableCollectionMetadata { data_shard, .. })| *data_shard)
            .collect();

        // Determine if there are any shards that belong to in-use collections
        // that we have marked for finalization. This is usually inconceivable,
        // but could happen if we e.g. crash during a migration.
        let in_use_shards_registered_for_finalization: BTreeSet<_> = shard_finalization
            .intersection(&in_use_shards)
            .cloned()
            .collect();

        // Fixup shard finalization WAL if necessary.
        if !in_use_shards_registered_for_finalization.is_empty() {
            self.clear_from_shard_finalization_register(in_use_shards_registered_for_finalization)
                .await;
        }

        // If we know about collections that the adapter has forgotten about, clean that up.
        if !leaked_collections.is_empty() {
            let mut shards_to_finalize = Vec::with_capacity(leaked_collections.len());
            let mut ids_to_drop = BTreeSet::new();

            for (id, DurableCollectionMetadata { data_shard, .. }) in leaked_collections {
                shards_to_finalize.push(data_shard);
                ids_to_drop.insert(id);
            }

            // Note that we register the shards for finalization but do not
            // finalize them here; this is meant to speed up startup times, as
            // we can defer actually finalizing shards.
            self.register_shards_for_finalization(shards_to_finalize)
                .await;

            let proto_ids_to_drop = ids_to_drop
                .into_iter()
                .map(|id| RustType::into_proto(&id))
                .collect();
            super::METADATA_COLLECTION
                .delete_keys(&mut self.state.stash, proto_ids_to_drop)
                .await
                .expect("stash operation must succeed");
        }
    }
}