forked from voldemort/voldemort
/
PartitionScanFetchEntriesRequestHandler.java
153 lines (131 loc) · 6.61 KB
/
PartitionScanFetchEntriesRequestHandler.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
/*
* Copyright 2008-2013 LinkedIn, Inc
*
* Licensed 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 voldemort.server.protocol.admin;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import voldemort.client.protocol.pb.ProtoUtils;
import voldemort.client.protocol.pb.VAdminProto;
import voldemort.client.protocol.pb.VAdminProto.FetchPartitionEntriesRequest;
import voldemort.server.StoreRepository;
import voldemort.server.VoldemortConfig;
import voldemort.store.ErrorCodeMapper;
import voldemort.store.metadata.MetadataStore;
import voldemort.store.stats.StreamingStats.Operation;
import voldemort.utils.ByteArray;
import voldemort.utils.ClosableIterator;
import voldemort.utils.NetworkClassLoader;
import voldemort.utils.Pair;
import voldemort.utils.StoreInstance;
import voldemort.versioning.Versioned;
import com.google.protobuf.Message;
/**
* Fetches entries using an efficient partition scan. Of course, only works if
* isPartitionScanSupported() is true for the storage engine to be scanned..
*
*/
public class FetchPartitionEntriesStreamRequestHandler extends FetchPartitionStreamRequestHandler {
protected ClosableIterator<Pair<ByteArray, Versioned<byte[]>>> entriesPartitionIterator;
public FetchPartitionEntriesStreamRequestHandler(FetchPartitionEntriesRequest request,
MetadataStore metadataStore,
ErrorCodeMapper errorCodeMapper,
VoldemortConfig voldemortConfig,
StoreRepository storeRepository,
NetworkClassLoader networkClassLoader) {
super(request,
metadataStore,
errorCodeMapper,
voldemortConfig,
storeRepository,
networkClassLoader,
Operation.FETCH_ENTRIES);
logger.info("Starting fetch entries for store '" + storageEngine.getName()
+ "' with replica to partition mapping " + replicaToPartitionList);
entriesPartitionIterator = null;
}
@Override
public StreamRequestHandlerState handleRequest(DataInputStream inputStream,
DataOutputStream outputStream)
throws IOException {
// process the next partition
if(entriesPartitionIterator == null) {
if(currentIndex == partitionList.size()) {
return StreamRequestHandlerState.COMPLETE;
}
// find the next partition to scan
boolean found = false;
while(!found && (currentIndex < partitionList.size())) {
currentPartition = new Integer(partitionList.get(currentIndex));
currentReplicaType = new Integer(replicaTypeList.get(currentIndex));
// Check the current node contains the partition as the
// requested replicatype
if(!fetchedPartitions.contains(currentPartition)
&& StoreInstance.checkPartitionBelongsToNode(currentPartition,
currentReplicaType,
nodeId,
initialCluster,
storeDef)) {
found = true;
fetchedPartitions.add(currentPartition);
partitionFetched = 0;
entriesPartitionIterator = storageEngine.entries(currentPartition);
statusInfoMessage("Starting fetch entries");
}
currentIndex++;
}
} else {
long startNs = System.nanoTime();
// do a check before reading in case partition has 0 elements
if(entriesPartitionIterator.hasNext()) {
Pair<ByteArray, Versioned<byte[]>> entry = entriesPartitionIterator.next();
ByteArray key = entry.getFirst();
Versioned<byte[]> value = entry.getSecond();
reportStorageOpTime(startNs);
throttler.maybeThrottle(key.length());
if(filter.accept(key, value)) {
recordFetched();
VAdminProto.FetchPartitionEntriesResponse.Builder response = VAdminProto.FetchPartitionEntriesResponse.newBuilder();
VAdminProto.PartitionEntry partitionEntry = VAdminProto.PartitionEntry.newBuilder()
.setKey(ProtoUtils.encodeBytes(key))
.setVersioned(ProtoUtils.encodeVersioned(value))
.build();
response.setPartitionEntry(partitionEntry);
Message message = response.build();
sendMessage(outputStream, message);
throttler.maybeThrottle(AdminServiceRequestHandler.valueSize(value));
}
scanned++;
if(0 == scanned % STAT_RECORDS_INTERVAL) {
progressInfoMessage("Fetch entries (progress)");
}
}
if(!entriesPartitionIterator.hasNext() || fetchedEnough(partitionFetched)) {
// Finished current partition. Reset iterator. Info status.
entriesPartitionIterator.close();
entriesPartitionIterator = null;
statusInfoMessage("Finished fetch keys");
progressInfoMessage("Fetch entries (end of partition)");
}
}
return StreamRequestHandlerState.WRITING;
}
@Override
public final void close(DataOutputStream outputStream) throws IOException {
if(null != entriesPartitionIterator)
entriesPartitionIterator.close();
super.close(outputStream);
}
}