use of io.s4.message.Response in project core by s4.
the class Handshake method clientInit.
private void clientInit(ByteArrayIOChannel io) throws IOException {
String uuid = UUID.randomUUID().toString();
Info proto = clientStub.getProtocolInfo();
HashMap<String, Object> info = new HashMap<String, Object>();
info.put("uuid", uuid);
info.put("protocol", proto);
String response = gson.toJson(info) + "\n";
io.send(response.getBytes());
}
use of io.s4.message.Response in project core by s4.
the class DefaultPartitioner method partition.
public List<CompoundKeyInfo> partition(String streamName, List<List<String>> compoundKeyNames, Object event, int partitionCount) {
if (streamName != null && streamNameSet != null && !streamNameSet.contains(streamName)) {
return null;
}
// Some event types that need special handling
if (event instanceof io.s4.message.Request) {
// construct key from request's target
io.s4.message.Request r = (io.s4.message.Request) event;
return r.partition(hasher, delimiter, partitionCount);
} else if (event instanceof io.s4.message.Response) {
// partition id is encoded in Response, so use it directly.
io.s4.message.Response r = (io.s4.message.Response) event;
return r.partition(partitionCount);
} else if (compoundKeyNames == null) {
// if compoundKeyNames is null, then assign to a random partition.
return partitionRandom(partitionCount);
}
// have to compute key value and
// partition based on hash of that value
Schema schema = schemaContainer.getSchema(event.getClass());
if (debug) {
System.out.println(schema);
}
List<CompoundKeyInfo> partitionInfoList = new ArrayList<CompoundKeyInfo>();
// fast path for single top-level key
if (fastPath || (compoundKeyNames.size() == 1 && compoundKeyNames.get(0).size() == 1)) {
String simpleKeyName = compoundKeyNames.get(0).get(0);
if (debug) {
System.out.println("Using fast path!");
}
fastPath = true;
KeyInfo keyInfo = new KeyInfo();
Property property = schema.getProperties().get(simpleKeyName);
if (property == null) {
return null;
}
Object value = null;
try {
value = property.getGetterMethod().invoke(event);
} catch (Exception e) {
if (debug) {
e.printStackTrace();
}
}
if (value == null) {
if (debug) {
System.out.println("Fast path: Null value encountered");
}
return null;
}
keyInfo.addElementToPath(simpleKeyName);
String stringValue = String.valueOf(value);
keyInfo.setValue(stringValue);
CompoundKeyInfo partitionInfo = new CompoundKeyInfo();
partitionInfo.addKeyInfo(keyInfo);
int partitionId = (int) (hasher.hash(stringValue) % partitionCount);
partitionInfo.setPartitionId(partitionId);
partitionInfo.setCompoundValue(stringValue);
partitionInfoList.add(partitionInfo);
if (debug) {
System.out.printf("Value %s, partition id %d\n", stringValue, partitionInfo.getPartitionId());
}
return partitionInfoList;
}
List<List<KeyInfo>> valueLists = new ArrayList<List<KeyInfo>>();
int maxSize = 0;
for (List<String> simpleKeyPath : compoundKeyNames) {
List<KeyInfo> keyInfoList = new ArrayList<KeyInfo>();
KeyInfo keyInfo = new KeyInfo();
keyInfoList = getKeyValues(event, schema, simpleKeyPath, 0, keyInfoList, keyInfo);
if (keyInfoList == null || keyInfoList.size() == 0) {
if (debug) {
System.out.println("Null value encountered");
}
// do no partitioning if any simple key's value
return null;
// resolves to null
}
valueLists.add(keyInfoList);
maxSize = Math.max(maxSize, keyInfoList.size());
if (debug) {
printKeyInfoList(keyInfoList);
}
}
for (int i = 0; i < maxSize; i++) {
String compoundValue = "";
CompoundKeyInfo partitionInfo = new CompoundKeyInfo();
for (List<KeyInfo> keyInfoList : valueLists) {
if (i < keyInfoList.size()) {
compoundValue += (compoundValue.length() > 0 ? delimiter : "") + keyInfoList.get(i).getValue();
partitionInfo.addKeyInfo(keyInfoList.get(i));
} else {
compoundValue += (compoundValue.length() > 0 ? delimiter : "") + keyInfoList.get(keyInfoList.size() - 1).getValue();
partitionInfo.addKeyInfo(keyInfoList.get(keyInfoList.size() - 1));
}
}
// get the partition id
int partitionId = (int) (hasher.hash(compoundValue) % partitionCount);
partitionInfo.setPartitionId(partitionId);
partitionInfo.setCompoundValue(compoundValue);
partitionInfoList.add(partitionInfo);
if (debug) {
System.out.printf("Value %s, partition id %d\n", compoundValue, partitionInfo.getPartitionId());
}
}
return partitionInfoList;
}
use of io.s4.message.Response in project rest.li by linkedin.
the class Http2FrameListener method onHeadersRead.
@Override
public void onHeadersRead(ChannelHandlerContext ctx, int streamId, Http2Headers headers, int padding, boolean endOfStream) throws Http2Exception {
LOG.debug("Received HTTP/2 HEADERS frame, stream={}, end={}, headers={}, padding={}bytes", new Object[] { streamId, endOfStream, headers.size(), padding });
// Ignores response for the upgrade request
if (streamId == Http2CodecUtil.HTTP_UPGRADE_STREAM_ID) {
return;
}
final StreamResponseBuilder builder = new StreamResponseBuilder();
// Process HTTP/2 pseudo headers
if (headers.status() != null) {
builder.setStatus(Integer.parseInt(headers.status().toString()));
}
if (headers.authority() != null) {
builder.addHeaderValue(HttpHeaderNames.HOST.toString(), headers.authority().toString());
}
// Process other HTTP headers
for (Map.Entry<CharSequence, CharSequence> header : headers) {
if (Http2Headers.PseudoHeaderName.isPseudoHeader(header.getKey())) {
// Do no set HTTP/2 pseudo headers to response
continue;
}
final String key = header.getKey().toString();
final String value = header.getValue().toString();
if (key.equalsIgnoreCase(HttpConstants.RESPONSE_COOKIE_HEADER_NAME)) {
builder.addCookie(value);
} else {
builder.unsafeAddHeaderValue(key, value);
}
}
// Gets async pool handle from stream properties
Http2Connection.PropertyKey handleKey = ctx.channel().attr(Http2ClientPipelineInitializer.CHANNEL_POOL_HANDLE_ATTR_KEY).get();
TimeoutAsyncPoolHandle<?> handle = _connection.stream(streamId).removeProperty(handleKey);
if (handle == null) {
_lifecycleManager.onError(ctx, Http2Exception.connectionError(Http2Error.PROTOCOL_ERROR, "No channel pool handle is associated with this stream", streamId));
return;
}
final StreamResponse response;
if (endOfStream) {
response = builder.build(EntityStreams.emptyStream());
ctx.fireChannelRead(handle);
} else {
// Associate an entity stream writer to the HTTP/2 stream
final TimeoutBufferedWriter writer = new TimeoutBufferedWriter(ctx, streamId, _maxContentLength, handle);
if (_connection.stream(streamId).setProperty(_writerKey, writer) != null) {
_lifecycleManager.onError(ctx, Http2Exception.connectionError(Http2Error.PROTOCOL_ERROR, "Another writer has already been associated with current stream ID", streamId));
return;
}
// Prepares StreamResponse for the channel pipeline
EntityStream entityStream = EntityStreams.newEntityStream(writer);
response = builder.build(entityStream);
}
// Gets callback from stream properties
Http2Connection.PropertyKey callbackKey = ctx.channel().attr(Http2ClientPipelineInitializer.CALLBACK_ATTR_KEY).get();
TransportCallback<?> callback = _connection.stream(streamId).removeProperty(callbackKey);
if (callback != null) {
ctx.fireChannelRead(new ResponseWithCallback<Response, TransportCallback<?>>(response, callback));
}
}
use of io.s4.message.Response in project core by s4.
the class ControlEventProcessor method execute.
protected void execute(EventWrapper e, PrototypeWrapper p) {
List<CompoundKeyInfo> keyInfoList = e.getCompoundKeys();
Object event = e.getEvent();
if (event instanceof SinglePERequest) {
// Handle Requests to individual PEs
if (keyInfoList.isEmpty())
return;
CompoundKeyInfo keyInfo = keyInfoList.get(0);
String keyVal = keyInfo.getCompoundValue();
ProcessingElement pe = p.lookupPE(keyVal);
Response response = ((SinglePERequest) event).evaluate(pe);
String stream = response.getRInfo().getStream();
dispatcher.dispatchEvent(stream, response);
} else if (event instanceof PrototypeRequest) {
// Or handle aggregate requests to Prototypes.
Response response = ((PrototypeRequest) event).evaluate(p);
String stream = response.getRInfo().getStream();
dispatcher.dispatchEvent(stream, response);
}
}
Aggregations