/
DistributionSchedule.java
246 lines (217 loc) · 7.26 KB
/
DistributionSchedule.java
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.bookkeeper.client;
import java.util.Map;
import org.apache.bookkeeper.net.BookieSocketAddress;
/**
* This interface determins how entries are distributed among bookies.
*
* <p>Every entry gets replicated to some number of replicas. The first replica for
* an entry is given a replicaIndex of 0, and so on. To distribute write load,
* not all entries go to all bookies. Given an entry-id and replica index, an
* {@link DistributionSchedule} determines which bookie that replica should go
* to.
*/
public interface DistributionSchedule {
/**
* A write set represents the set of bookies to which
* a request will be written.
* The set consists of a list of indices which can be
* used to lookup the bookie in the ensemble.
*/
interface WriteSet {
/**
* The number of indexes in the write set.
*/
int size();
/**
* Whether the set contains the given index.
*/
boolean contains(int i);
/**
* Get the index at index i.
*/
int get(int i);
/**
* Set the index at index i.
* @return the previous value at that index.
*/
int set(int i, int index);
/**
* Sort the indices.
*/
void sort();
/**
* Index of a specified bookie index.
* -1 if not found.
*/
int indexOf(int index);
/**
* If we want a write set to cover all bookies in an ensemble
* of size X, then all of the index from 0..X must exist in the
* write set. This method appends those which are missing to the
* end of the write set.
*/
void addMissingIndices(int maxIndex);
/**
* Move an index from one position to another,
* shifting the other indices accordingly.
*/
void moveAndShift(int from, int to);
/**
* Recycle write set object when not in use.
*/
void recycle();
/**
* Make a deep copy of this write set.
*/
WriteSet copy();
}
WriteSet NULL_WRITE_SET = new WriteSet() {
@Override
public int size() {
return 0;
}
@Override
public boolean contains(int i) {
return false;
}
@Override
public int get(int i) {
throw new ArrayIndexOutOfBoundsException();
}
@Override
public int set(int i, int index) {
throw new ArrayIndexOutOfBoundsException();
}
@Override
public void sort() {}
@Override
public int indexOf(int index) {
return -1;
}
@Override
public void addMissingIndices(int maxIndex) {
throw new ArrayIndexOutOfBoundsException();
}
@Override
public void moveAndShift(int from, int to) {
throw new ArrayIndexOutOfBoundsException();
}
@Override
public void recycle() {}
@Override
public WriteSet copy() {
return this;
}
};
/**
* Return the set of bookie indices to send the message to.
*/
WriteSet getWriteSet(long entryId);
/**
* Return the set of bookies indices to send the messages to for longpoll reads.
*
* @param entryId expected next entry id to read.
* @return the set of bookies indices to read from.
*/
WriteSet getWriteSetForLongPoll(long entryId);
/**
* Return the set of bookies indices to send the messages to for force ledger.
*
* @return the set of bookies indices to send force request to.
*/
WriteSet getWriteSetForForceLedger();
/**
* An ack set represents the set of bookies from which
* a response must be received so that an entry can be
* considered to be replicated on a quorum.
*/
interface AckSet {
/**
* Add a bookie response and check if quorum has been met.
* @return true if quorum has been met, false otherwise
*/
boolean completeBookieAndCheck(int bookieIndexHeardFrom);
/**
* Received failure response from a bookie and check if ack quorum
* will be broken.
*
* @param bookieIndexHeardFrom
* bookie index that failed.
* @param address
* bookie address
* @return true if ack quorum is broken, false otherwise.
*/
boolean failBookieAndCheck(int bookieIndexHeardFrom, BookieSocketAddress address);
/**
* Return the list of bookies that already failed.
*
* @return the list of bookies that already failed.
*/
Map<Integer, BookieSocketAddress> getFailedBookies();
/**
* Invalidate a previous bookie response.
* Used for reissuing write requests.
*/
boolean removeBookieAndCheck(int bookie);
/**
* Recycle this ack set when not used anymore.
*/
void recycle();
}
/**
* Returns an ackset object, responses should be checked against this.
*/
AckSet getAckSet();
/**
* Returns an ackset object useful to wait for all bookies in the ensemble,
* responses should be checked against this.
*/
AckSet getAckSetForForceLedger();
/**
* Interface to keep track of which bookies in an ensemble, an action
* has been performed for.
*/
interface QuorumCoverageSet {
/**
* Add a bookie to the result set.
*
* @param bookieIndexHeardFrom Bookie we've just heard from
*/
void addBookie(int bookieIndexHeardFrom, int rc);
/**
* check if all quorum in the set have had the action performed for it.
*
* @return whether all quorums have been covered
*/
boolean checkCovered();
}
QuorumCoverageSet getCoverageSet();
/**
* Whether entry presents on given bookie index.
*
* @param entryId
* - entryId to check the presence on given bookie index
* @param bookieIndex
* - bookie index on which it need to check the possible presence
* of the entry
* @return true if it has entry otherwise false.
*/
boolean hasEntry(long entryId, int bookieIndex);
}