use of com.google.protobuf.Message in project heron by twitter.
the class HeronClient method handlePacket.
/**
* Handle an incomingPacket and if necessary,
* convert it to Message and call onIncomingMessage() to handle it
*/
protected void handlePacket(IncomingPacket incomingPacket) {
String typeName = incomingPacket.unpackString();
REQID rid = incomingPacket.unpackREQID();
if (contextMap.containsKey(rid)) {
// This incomingPacket contains the response of Request
Object ctx = contextMap.get(rid);
Message.Builder bldr = responseMessageMap.get(rid);
contextMap.remove(rid);
responseMessageMap.remove(rid);
incomingPacket.unpackMessage(bldr);
// Call onResponse to handle it
if (bldr.isInitialized()) {
Message response = bldr.build();
onResponse(StatusCode.OK, ctx, response);
return;
} else {
onResponse(StatusCode.INVALID_PACKET, ctx, null);
return;
}
} else if (rid.equals(REQID.zeroREQID)) {
// If rid is REQID.zeroREQID, this is a Message, e.g. no need send back response.
// Convert it into message and call onIncomingMessage() to handle it
Message.Builder bldr = messageMap.get(typeName);
if (bldr != null) {
bldr.clear();
incomingPacket.unpackMessage(bldr);
if (bldr.isInitialized()) {
onIncomingMessage(bldr.build());
} else {
// We just need to log here
// TODO:- log
}
} else {
// We got a message but we didn't register
// TODO:- log here
}
} else {
// This might be a timeout response
// TODO:- log here
}
}
use of com.google.protobuf.Message in project heron by twitter.
the class CuratorStateManagerTest method testGetNodeData.
/**
* Test getNodeData method
* @throws Exception
*/
@Test
public void testGetNodeData() throws Exception {
CuratorStateManager spyStateManager = spy(new CuratorStateManager());
final CuratorFramework mockClient = mock(CuratorFramework.class);
GetDataBuilder mockGetBuilder = mock(GetDataBuilder.class);
// Mockito doesn't support mock type-parametrized class, thus suppress the warning
@SuppressWarnings("rawtypes") BackgroundPathable mockBackPathable = mock(BackgroundPathable.class);
final CuratorEvent mockEvent = mock(CuratorEvent.class);
Message.Builder mockBuilder = mock(Message.Builder.class);
Message mockMessage = mock(Message.class);
final byte[] data = "wy_1989".getBytes();
doReturn(mockMessage).when(mockBuilder).build();
doReturn(data).when(mockEvent).getData();
doReturn(PATH).when(mockEvent).getPath();
doReturn(mockClient).when(spyStateManager).getCuratorClient();
doReturn(true).when(mockClient).blockUntilConnected(anyInt(), any(TimeUnit.class));
doReturn(mockGetBuilder).when(mockClient).getData();
doReturn(mockBackPathable).when(mockGetBuilder).usingWatcher(any(Watcher.class));
doAnswer(new Answer<Object>() {
@Override
public Object answer(InvocationOnMock invocationOnMock) throws Throwable {
Object[] objests = invocationOnMock.getArguments();
// the first object is the BackgroundCallback
((BackgroundCallback) objests[0]).processResult(mockClient, mockEvent);
return null;
}
}).when(mockBackPathable).inBackground(any(BackgroundCallback.class));
spyStateManager.initialize(config);
// Verify the data on node is fetched correctly
ListenableFuture<Message> result = spyStateManager.getNodeData(null, PATH, mockBuilder);
assertTrue(result.get().equals(mockMessage));
}
use of com.google.protobuf.Message in project voldemort by voldemort.
the class FullScanFetchEntriesRequestHandler method handleRequest.
@Override
public StreamRequestHandlerState handleRequest(DataInputStream inputStream, DataOutputStream outputStream) throws IOException {
if (!keyIterator.hasNext()) {
return StreamRequestHandlerState.COMPLETE;
}
// NOTE: Storage time is accounted for somewhat incorrectly because
// .hasNext() is invoked at end of method for the common case.
// Since key reading (keyIterator.next()) is done separately from entry
// fetching (storageEngine.get()), must be careful about when to invoke
// reportStorageOpTime and when to invoke maybeThrottle().
long startNs = System.nanoTime();
ByteArray key = keyIterator.next();
if (isItemAccepted(key.get())) {
List<Versioned<byte[]>> values = storageEngine.get(key, null);
reportStorageOpTime(startNs);
throttler.maybeThrottle(key.length());
for (Versioned<byte[]> value : values) {
if (filter.accept(key, value)) {
accountForFetchedKey(key.get());
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));
}
}
} else {
reportStorageOpTime(startNs);
throttler.maybeThrottle(key.length());
}
accountForScanProgress("entries");
return determineRequestHandlerState("entries");
}
use of com.google.protobuf.Message in project voldemort by voldemort.
the class PartitionScanFetchEntriesRequestHandler method handleRequest.
@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));
// Check the current node contains the partition
if (!fetchedPartitions.contains(currentPartition) && StoreRoutingPlan.checkPartitionBelongsToNode(currentPartition, nodeId, initialCluster, storeDef)) {
found = true;
completedFetchingCurrentPartition();
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));
}
accountForScanProgress("entries");
}
if (!entriesPartitionIterator.hasNext() || fetchedEnoughForCurrentPartition()) {
// Finished current partition. Reset iterator. Info status.
entriesPartitionIterator.close();
entriesPartitionIterator = null;
statusInfoMessage("Finished fetch keys");
progressInfoMessage("Fetch entries (end of partition)");
}
}
return StreamRequestHandlerState.WRITING;
}
use of com.google.protobuf.Message in project voldemort by voldemort.
the class PartitionScanFetchKeysRequestHandler method handleRequest.
@Override
public StreamRequestHandlerState handleRequest(DataInputStream inputStream, DataOutputStream outputStream) throws IOException {
// process the next partition
if (keysPartitionIterator == null) {
if (currentIndex == partitionList.size()) {
return StreamRequestHandlerState.COMPLETE;
}
// find the next partition to scan and set currentIndex.
boolean found = false;
while (!found && (currentIndex < partitionList.size())) {
currentPartition = partitionList.get(currentIndex);
if (!fetchedPartitions.contains(currentPartition) && StoreRoutingPlan.checkPartitionBelongsToNode(currentPartition, nodeId, initialCluster, storeDef)) {
found = true;
completedFetchingCurrentPartition();
keysPartitionIterator = storageEngine.keys(currentPartition);
statusInfoMessage("Starting fetch keys");
}
currentIndex++;
}
} else {
long startNs = System.nanoTime();
// do a check before reading in case partition has 0 elements
if (keysPartitionIterator.hasNext()) {
ByteArray key = keysPartitionIterator.next();
reportStorageOpTime(startNs);
throttler.maybeThrottle(key.length());
if (filter.accept(key, null)) {
recordFetched();
VAdminProto.FetchPartitionEntriesResponse.Builder response = VAdminProto.FetchPartitionEntriesResponse.newBuilder();
response.setKey(ProtoUtils.encodeBytes(key));
Message message = response.build();
sendMessage(outputStream, message);
}
accountForScanProgress("keys");
}
if (!keysPartitionIterator.hasNext() || fetchedEnoughForCurrentPartition()) {
// Finished current partition. Reset iterator. Info status.
keysPartitionIterator.close();
keysPartitionIterator = null;
statusInfoMessage("Finished fetch keys");
progressInfoMessage("Fetch keys (end of partition)");
}
}
return StreamRequestHandlerState.WRITING;
}
Aggregations