/** Return the protocol for a Java interface. */ public Protocol getProtocol(Class iface) { try { Protocol p = (Protocol)(iface.getDeclaredField("PROTOCOL").get(null)); if (!p.getNamespace().equals(iface.getPackage().getName())) // HACK: protocol mismatches iface. maven shade plugin? try replacing. p = Protocol.parse(p.toString().replace(p.getNamespace(), iface.getPackage().getName())); return p; } catch (NoSuchFieldException e) { throw new AvroRuntimeException("Not a Specific protocol: "+iface); } catch (IllegalAccessException e) { throw new AvroRuntimeException(e); } }
@Test public void testEcho() throws IOException { GenericRecord record = new GenericData.Record(PROTOCOL.getType("TestRecord")); record.put("name", new Utf8("foo")); record.put("kind", new GenericData.EnumSymbol (PROTOCOL.getType("Kind"), "BAR")); record.put("hash", new GenericData.Fixed (PROTOCOL.getType("MD5"), new byte[]{0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5})); GenericRecord params = new GenericData.Record(PROTOCOL.getMessages().get("echo").getRequest()); params.put("record", record); Object echoed = requestor.request("echo", params); assertEquals(record, echoed); }
private Protocol addStringType(Protocol p) { if (stringType != StringType.String) return p; Protocol newP = new Protocol(p.getName(), p.getDoc(), p.getNamespace()); Map<Schema,Schema> types = new LinkedHashMap<>(); for (Map.Entry<String, Object> a : p.getObjectProps().entrySet()) { newP.addProp(a.getKey(), a.getValue()); } // annotate types Collection<Schema> namedTypes = new LinkedHashSet<>(); for (Schema s : p.getTypes()) namedTypes.add(addStringType(s, types)); newP.setTypes(namedTypes); // annotate messages Map<String,Message> newM = newP.getMessages(); for (Message m : p.getMessages().values()) newM.put(m.getName(), m.isOneWay() ? newP.createMessage(m, addStringType(m.getRequest(), types)) : newP.createMessage(m, addStringType(m.getRequest(), types), addStringType(m.getResponse(), types), addStringType(m.getErrors(), types))); return newP; }
/** * Constructs a similar Protocol instance with the same {@code name}, {@code doc}, and {@code namespace} as {code p} * has. It also copies all the {@code props}. */ public Protocol(Protocol p) { this(p.getName(), p.getDoc(), p.getNamespace()); putAll(p); }
/** * Provides a a unique gRPC service name for Avro RPC interface or its subclass Callback * Interface. * * @param iface Avro RPC interface. * @return unique service name for gRPC. */ public static String getServiceName(Class iface) { Protocol protocol = getProtocol(iface); return protocol.getNamespace() + "." + protocol.getName(); }
@Test public void testP2() throws Exception { Schema e1 = ReflectData.get().getSchema(E1.class); assertEquals(Schema.Type.RECORD, e1.getType()); assertTrue(e1.isError()); Field message = e1.getField("detailMessage"); assertNotNull("field 'detailMessage' should not be null", message); Schema messageSchema = message.schema(); assertEquals(Schema.Type.UNION, messageSchema.getType()); assertEquals(Schema.Type.NULL, messageSchema.getTypes().get(0).getType()); assertEquals(Schema.Type.STRING, messageSchema.getTypes().get(1).getType()); Protocol p2 = ReflectData.get().getProtocol(P2.class); Protocol.Message m = p2.getMessages().get("error"); // check error schema is union Schema response = m.getErrors(); assertEquals(Schema.Type.UNION, response.getType()); assertEquals(Schema.Type.STRING, response.getTypes().get(0).getType()); assertEquals(e1, response.getTypes().get(1)); }
/** Test that Responder ignores one-way with stateless transport. */ @Test public void testStatelessOneway() throws Exception { // a version of the Simple protocol that doesn't declare "ack" one-way Protocol protocol = new Protocol("Simple", "org.apache.avro.test"); Protocol.Message message = protocol.createMessage("ack", null, Schema.createRecord(new ArrayList<>()), Schema.create(Schema.Type.NULL), Schema.createUnion(new ArrayList<>())); protocol.getMessages().put("ack", message); // call a server over a stateless protocol that has a one-way "ack" GenericRequestor requestor = new GenericRequestor(protocol, createTransceiver()); requestor.request("ack", new GenericData.Record(message.getRequest())); // make the request again, to better test handshakes w/ differing protocols requestor.request("ack", new GenericData.Record(message.getRequest())); }
for (Field f : PROTOCOL.getType("TestRecord").getFields()) fields.add(new Field(f.name(), f.schema(), null, null)); fields.add(new Field("extra", Schema.create(Schema.Type.BOOLEAN), null, true)); Schema record = Schema.createRecord("TestRecord", null, "org.apache.avro.test", false); record.setFields(fields); Protocol protocol = new Protocol("Simple", "org.apache.avro.test"); List<Field> params = new ArrayList<>(); params.add(new Field("record", record, null, null)); protocol.createMessage("echo", null, Schema.createRecord(params), record, Schema.createUnion(new ArrayList<>())); protocol.getMessages().put("echo", message); Transceiver t = new SocketTransceiver(new InetSocketAddress(server.getPort())); try { GenericRequestor r = new GenericRequestor(protocol, t); GenericRecord args = new GenericData.Record(message.getRequest()); GenericRecord rec = new GenericData.Record(record); rec.put("name", new Utf8("foo")); rec.put("kind", new GenericData.EnumSymbol (PROTOCOL.getType("Kind"), "BAR")); rec.put("hash", new GenericData.Fixed (PROTOCOL.getType("MD5"), new byte[]{0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5})); rec.put("extra", Boolean.TRUE);
@Override public String getProperty(String key) throws Exception { if(key == null) { throw new NullPointerException("key"); } Schema schema = _protocol.getMessages().get(ProtocolConstants.MSG_META).getRequest(); GenericRecord req = new GenericData.Record(schema); req.put("src", _sourceUtf8); req.put("opt", ProtocolConstants.OPT_GET_PROPERTY_UTF8); req.put("key", new Utf8(key)); Utf8 res = (Utf8)send(ProtocolConstants.MSG_META, req); return res == null ? null : res.toString(); }
Annotation annotation = annotations[i][j]; if (annotation instanceof AvroSchema) // explicit schema paramSchema = Schema.parse(((AvroSchema)annotation).value()); else if (annotation instanceof Union) // union paramSchema = getAnnotatedUnion(((Union)annotation), names); : paramSchema.getName()+i; fields.add(new Schema.Field(paramName, paramSchema, Schema request = Schema.createRecord(fields); errs.add(getSchema(err, names)); Schema errors = Schema.createUnion(errs); return protocol.createMessage(method.getName(), null /* doc */, request, response, errors);
@Test public void testHello() throws IOException { GenericRecord params = new GenericData.Record(PROTOCOL.getMessages().get("hello").getRequest()); params.put("greeting", new Utf8("bob")); Utf8 response = (Utf8)requestor.request("hello", params); assertEquals(new Utf8("goodbye"), response); }
public Object respond(Message message, Object request) throws AvroRemoteException { GenericRecord params = (GenericRecord)request; if ("hello".equals(message.getName())) { LOG.info("hello: "+params.get("greeting")); return new Utf8("goodbye"); } if ("echo".equals(message.getName())) { Object record = params.get("record"); LOG.info("echo: "+record); return record; } if ("echoBytes".equals(message.getName())) { Object data = params.get("data"); LOG.info("echoBytes: "+data); return data; } if ("error".equals(message.getName())) { if (throwUndeclaredError) throw new RuntimeException("foo"); GenericRecord error = new GenericData.Record(PROTOCOL.getType("TestError")); error.put("message", new Utf8("an error")); throw new AvroRemoteException(error); } throw new AvroRuntimeException("unexpected message: "+message.getName()); }
@Override public void addIdAsDouble(double id) { this.idRecord = new GenericData.Record( protocol.getType( "Id" ) ); idRecord.put( "value", id ); }
private void processProtoFile(File sourceFile) { getLogger().info("Processing {}", sourceFile); try { Protocol protocol = Protocol.parse(sourceFile); for (Schema schema : protocol.getTypes()) { String path = schema.getNamespace().replaceAll(Pattern.quote("."), "/"); File schemaFile = new File(getOutputDir(), path + "/" + schema.getName() + "." + SCHEMA_EXTENSION); String schemaJson = schema.toString(true); FileUtils.writeJsonFile(schemaFile, schemaJson); getLogger().debug("Wrote {}", schemaFile.getPath()); } } catch (IOException ex) { throw new GradleException(String.format("Failed to process protocol definition file %s", sourceFile), ex); } } }
@Test public void testP4() throws Exception { Protocol p = ReflectData.get().getProtocol(P4.class); Protocol.Message message = p.getMessages().get("foo"); assertEquals(Schema.Type.INT, message.getResponse().getType()); Field field = message.getRequest().getField("x"); assertEquals(Schema.Type.INT, field.schema().getType()); }
Schema schemaKV = _protocol.getType(ProtocolConstants.TYPE_KeyValue); Schema schema = _protocol.getMessages().get(ProtocolConstants.MSG_MPUT).getRequest(); GenericRecord req = new GenericData.Record(schema); GenericArray<GenericRecord> array = new GenericData.Array<GenericRecord>(map.size(), schema.getField("kvList").schema()); GenericRecord item = new GenericData.Record(schemaKV); K key = e.getKey(); V value = e.getValue(); item.put("key", serializeKey(key)); item.put("value", value == null ? null : serializeValue(value)); array.add(item); req.put("src", _sourceUtf8); req.put("kvList", array);
@Override public boolean delete(Collection<K> keys) throws Exception { if(keys == null) { throw new NullPointerException("keys"); } Schema schema = _protocol.getMessages().get(ProtocolConstants.MSG_MDEL).getRequest(); GenericRecord req = new GenericData.Record(schema); GenericArray<ByteBuffer> array = new GenericData.Array<ByteBuffer>(keys.size(), schema.getField("keys").schema()); for(K key : keys) { if(key != null) { array.add(serializeKey(key)); } } if(array.size() == 0) { return false; } req.put("src", _sourceUtf8); req.put("keys", array); return (Boolean)send(ProtocolConstants.MSG_MDEL, req); }
Schema errors = Schema.createUnion(errorSchemata); if (oneWay && response.getType() != Type.NULL) {if (true) throw error("One-way message'"+name+"' must return void", token);} {if (true) return oneWay ? p.createMessage(name, msgDoc, props, request) : p.createMessage(name, msgDoc, props, request, response, errors);} throw new Error("Missing return statement in function");