I get the following error when trying to insert some nodes into dgraph.
com.google.protobuf.InvalidProtocolBufferException: Protocol message had invalid UTF-8.
I narrowed it down to
ManagedChannel channel = ManagedChannelBuilder.forAddress("localhost", 9080).usePlaintext(true).build();
DgraphGrpc.DgraphBlockingStub blockingStub = DgraphGrpc.newBlockingStub(channel);
DgraphClient dgraphClient = new DgraphClient(Collections.singletonList(blockingStub));
for(int i = 0; i < 1000; i++) {
JsonObject json = new JsonObject();
json.addProperty("id", i);
json.addProperty("name", "abcdefgh" + i);
System.out.println("for " + i);
Mutation mu =
Mutation.newBuilder()
.setCommitImmediately(true)
.setSetJson(ByteString.copyFromUtf8(json.toString()))
.build();
dgraphClient.newTransaction().mutate(mu);
}
After inserting around 100 to 150 nodes. I get the above error. If I rerun the above code, I get the same error for the first record itself. Every subsequent mutation request gets the same error.
note the repeated string keys = 4. This means that (according to the proto language spec) the keys field must be a valid UTF-8 string . But the server currently sends a stream of bytes without bothering to check if it is valid UTF-8. The java client (autogenerated code) tries to decode the stream as valid UTF-8 and throws an error as it is not able to decode it as valid UTF-8.
This problem does not show up in the Go client, because it just constructs a string using a byte slice returned from the server. It does not bother decoding it.
The fix is to base64 encode the strings before sending them, so that they are guaranteed to be valid UTF-8. This will require a fix to the server. Once it is fixed you will need to run the latest Dgraph server. The client code should not need to change.