下面列出了com.google.protobuf.TextFormat.ParseException#com.google.protobuf.TextFormat 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
private Pipeline.TrainEvalPipelineConfig parse(Path path) {
try {
String text = new String(Files.readAllBytes(path), StandardCharsets.UTF_8);
Pipeline.TrainEvalPipelineConfig.Builder builder = Pipeline.TrainEvalPipelineConfig.newBuilder();
TextFormat.Parser parser = TextFormat.Parser.newBuilder().build();
// Skip unknown fields
Field f = parser.getClass().getDeclaredField("allowUnknownFields");
f.setAccessible(true);
f.set(parser, true);
parser.merge(text, builder);
return builder.build();
}
catch (Exception ex) {
LOG.log(Level.SEVERE, "Unable to parse pipeline", ex);
}
return null;
}
void write(ZooKeeper zkc, String path)
throws IOException, KeeperException.NodeExistsException {
this.zkPath = path;
EditLogLedgerProto.Builder builder = EditLogLedgerProto.newBuilder();
builder.setDataLayoutVersion(dataLayoutVersion)
.setLedgerId(ledgerId).setFirstTxId(firstTxId);
if (!inprogress) {
builder.setLastTxId(lastTxId);
}
try {
zkc.create(path, TextFormat.printToString(builder.build()).getBytes(UTF_8),
Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
} catch (KeeperException.NodeExistsException nee) {
throw nee;
} catch (KeeperException e) {
throw new IOException("Error creating ledger znode", e);
} catch (InterruptedException ie) {
throw new IOException("Interrupted creating ledger znode", ie);
}
}
/**
* @return the current state of the given segment, or null if the
* segment does not exist.
*/
@VisibleForTesting
SegmentStateProto getSegmentInfo(long segmentTxId)
throws IOException {
EditLogFile elf = fjm.getLogFile(segmentTxId);
if (elf == null) {
return null;
}
if (elf.isInProgress()) {
elf.scanLog();
}
if (elf.getLastTxId() == HdfsConstants.INVALID_TXID) {
LOG.info("Edit log file " + elf + " appears to be empty. " +
"Moving it aside...");
elf.moveAsideEmptyFile();
return null;
}
SegmentStateProto ret = SegmentStateProto.newBuilder()
.setStartTxId(segmentTxId)
.setEndTxId(elf.getLastTxId())
.setIsInProgress(elf.isInProgress())
.build();
LOG.info("getSegmentInfo(" + segmentTxId + "): " + elf + " -> " +
TextFormat.shortDebugString(ret));
return ret;
}
private void writeData(Message message) throws IOException {
switch (outputType) {
case BINARY:
message.writeTo(outputStream);
break;
case TEXT:
TextFormat.print(message, printStream);
break;
case JSON:
jsonPrinter.appendTo(message, printStream);
printStream.append('\n');
break;
default:
throw new IllegalStateException("Unknown outputType " + outputType.formatName());
}
}
/**
* Maps the provided status to the service that owns its task, then queries that service with the status.
*
* <p>This is an optimization which avoids querying services about task statuses that don't relate to them.
* <p>In addition to reducing unnecessary queries, this also improves isolation between services. They only see
* task statuses which relate to them.
*/
@Override
public TaskStatusResponse taskStatus(Protos.TaskStatus status) {
return multiServiceManager
.getMatchingService(status)
.map(x -> x.taskStatus(status))
.orElseGet(() -> multiServiceManager
.getServiceSanitized(frameworkName)
.map(x -> {
LOGGER.info("Forwarding task status to default service: {}", frameworkName);
return x.taskStatus(status);
})
.orElseGet(() -> {
// Unrecognized service. Status for old task ?
LOGGER.info("Received status for unknown task {}: {}",
status.getTaskId().getValue(), TextFormat.shortDebugString(status));
return TaskStatusResponse.unknownTask();
})
);
}
/** Tests the whole process of editing manifest to catch any unintended changes. */
@Test
public void complexManifest_featureSplit() throws Exception {
XmlNode.Builder xmlNodeBuilder = XmlNode.newBuilder();
TextFormat.merge(TestData.openReader("testdata/manifest/manifest1.textpb"), xmlNodeBuilder);
AndroidManifest androidManifest = AndroidManifest.create(xmlNodeBuilder.build());
XmlNode.Builder expectedXmlNodeBuilder = XmlNode.newBuilder();
TextFormat.merge(
TestData.openReader("testdata/manifest/feature_split_manifest1.textpb"),
expectedXmlNodeBuilder);
XmlNode generatedManifest =
androidManifest
.toEditor()
.setSplitIdForFeatureSplit("testModule")
.save()
.getManifestRoot()
.getProto();
assertThat(generatedManifest).isEqualTo(expectedXmlNodeBuilder.build());
}
/**
* Convert DAGPlan to text. Skip sensitive informations like credentials.
*
* @param dagPlan
* @return a string representation of the dag plan with sensitive information removed
*/
public static String convertDagPlanToString(DAGProtos.DAGPlan dagPlan) throws IOException {
StringBuilder sb = new StringBuilder();
for (Map.Entry<Descriptors.FieldDescriptor, Object> entry : dagPlan.getAllFields().entrySet()) {
if (entry.getKey().getNumber() != DAGProtos.DAGPlan.CREDENTIALS_BINARY_FIELD_NUMBER) {
TextFormat.printField(entry.getKey(), entry.getValue(), sb);
} else {
Credentials credentials =
DagTypeConverters.convertByteStringToCredentials(dagPlan.getCredentialsBinary());
TextFormat.printField(entry.getKey(),
ByteString.copyFrom(TezCommonUtils.getCredentialsInfo(credentials,"dag").getBytes(
Charset.forName("UTF-8"))), sb);
}
}
return sb.toString();
}
/**
* @return the current state of the given segment, or null if the
* segment does not exist.
*/
@VisibleForTesting
SegmentStateProto getSegmentInfo(long segmentTxId)
throws IOException {
EditLogFile elf = fjm.getLogFile(segmentTxId);
if (elf == null) {
return null;
}
if (elf.isInProgress()) {
elf.scanLog();
}
if (elf.getLastTxId() == HdfsConstants.INVALID_TXID) {
LOG.info("Edit log file " + elf + " appears to be empty. " +
"Moving it aside...");
elf.moveAsideEmptyFile();
return null;
}
SegmentStateProto ret = SegmentStateProto.newBuilder()
.setStartTxId(segmentTxId)
.setEndTxId(elf.getLastTxId())
.setIsInProgress(elf.isInProgress())
.build();
LOG.info("getSegmentInfo(" + segmentTxId + "): " + elf + " -> " +
TextFormat.shortDebugString(ret));
return ret;
}
private void handlePacketIn(P4RuntimeOuterClass.PacketIn packetInMsg) {
if (log.isTraceEnabled()) {
log.trace("Received packet-in from {}: {}", deviceId, packetInMsg);
}
if (!pipeconfService.getPipeconf(deviceId).isPresent()) {
log.warn("Unable to handle packet-in from {}, missing pipeconf: {}",
deviceId, TextFormat.shortDebugString(packetInMsg));
return;
}
// Decode packet message and post event.
// TODO: consider implementing a cache to speed up
// encoding/deconding of packet-in/out (e.g. LLDP, ARP)
final PiPipeconf pipeconf = pipeconfService.getPipeconf(deviceId).get();
final PiPacketOperation pktOperation;
try {
pktOperation = CODECS.packetIn().decode(
packetInMsg, null, pipeconf);
} catch (CodecException e) {
log.warn("Unable to process packet-int: {}", e.getMessage());
return;
}
controller.postEvent(new P4RuntimeEvent(
P4RuntimeEvent.Type.PACKET_IN,
new PacketInEvent(deviceId, pktOperation)));
}
public void save() throws IOException {
if (config == null) {
return;
}
config = update(config);
String pipelineConfig = TextFormat.printToString(config);
Files.write(getModelConfigPath(baseModelDir), pipelineConfig.getBytes());
LOG.info("Created "+ getModelConfigPath(baseModelDir));
}
@Override
protected void writeInternal(Message message, HttpOutputMessage outputMessage)
throws IOException, HttpMessageNotWritableException {
MediaType contentType = outputMessage.getHeaders().getContentType();
if (contentType == null) {
contentType = getDefaultContentType(message);
Assert.state(contentType != null, "No content type");
}
Charset charset = contentType.getCharset();
if (charset == null) {
charset = DEFAULT_CHARSET;
}
if (PROTOBUF.isCompatibleWith(contentType)) {
setProtoHeader(outputMessage, message);
CodedOutputStream codedOutputStream = CodedOutputStream.newInstance(outputMessage.getBody());
message.writeTo(codedOutputStream);
codedOutputStream.flush();
}
else if (TEXT_PLAIN.isCompatibleWith(contentType)) {
OutputStreamWriter outputStreamWriter = new OutputStreamWriter(outputMessage.getBody(), charset);
TextFormat.print(message, outputStreamWriter);
outputStreamWriter.flush();
outputMessage.getBody().flush();
}
else if (this.protobufFormatSupport != null) {
this.protobufFormatSupport.print(message, outputMessage.getBody(), contentType, charset);
outputMessage.getBody().flush();
}
}
/** Reads a prototxt file into a proto from zip archive. */
public Message readPrototxtFromZip(
String zipFilePath, String pathInsideZip, Message.Builder builder) throws IOException {
ZipFile zipFile =
new ZipFile(expandHomeDirectory(joinPaths(getCurrentWorkingDirectory(), zipFilePath)));
String content = streamToString(zipFile.getInputStream(zipFile.getEntry(pathInsideZip)));
TextFormat.merge(content, builder);
return builder.build();
}
/** Creates a CcToolchainFeatures from features described in the given toolchain fragment. */
public static CcToolchainFeatures buildFeatures(RuleContext ruleContext, String... toolchain)
throws Exception {
CToolchain.Builder toolchainBuilder = CToolchain.newBuilder();
TextFormat.merge(Joiner.on("").join(toolchain), toolchainBuilder);
return new CcToolchainFeatures(
CcToolchainConfigInfo.fromToolchain(toolchainBuilder.buildPartial()),
/* ccToolchainPath= */ PathFragment.EMPTY_FRAGMENT);
}
public Service.Request transform(ByteString serializedMessage) throws
InvalidProtocolBufferException {
// This should already be an aliased CodedInputStream from the WireMessage parsing.
Message msg = parser.parseFrom(serializedMessage.newCodedInput());
if (LOG.isTraceEnabled()) {
LOG.trace("Deserialized request '{}'", TextFormat.shortDebugString(msg));
}
return impl.deserialize(msg);
}
@Override
protected void writeInternal(Message message, HttpOutputMessage outputMessage)
throws IOException, HttpMessageNotWritableException {
MediaType contentType = outputMessage.getHeaders().getContentType();
if (contentType == null) {
contentType = getDefaultContentType(message);
}
Charset charset = contentType.getCharset();
if (charset == null) {
charset = DEFAULT_CHARSET;
}
if (MediaType.TEXT_PLAIN.isCompatibleWith(contentType)) {
OutputStreamWriter outputStreamWriter = new OutputStreamWriter(outputMessage.getBody(), charset);
TextFormat.print(message, outputStreamWriter);
outputStreamWriter.flush();
}
else if (MediaType.APPLICATION_JSON.isCompatibleWith(contentType)) {
JSON_FORMAT.print(message, outputMessage.getBody(), charset);
}
else if (MediaType.APPLICATION_XML.isCompatibleWith(contentType)) {
XML_FORMAT.print(message, outputMessage.getBody(), charset);
}
else if (MediaType.TEXT_HTML.isCompatibleWith(contentType)) {
HTML_FORMAT.print(message, outputMessage.getBody(), charset);
}
else if (PROTOBUF.isCompatibleWith(contentType)) {
setProtoHeader(outputMessage, message);
FileCopyUtils.copy(message.toByteArray(), outputMessage.getBody());
}
}
@Override
public void statusUpdate(SchedulerDriver driver, Protos.TaskStatus status) {
try {
LOGGER.info("Received status update for taskId={} state={} message={} protobuf={}",
status.getTaskId().getValue(),
status.getState().toString(),
status.getMessage(),
TextFormat.shortDebugString(status));
Metrics.record(status);
TaskStatusResponse response = mesosEventClient.taskStatus(status);
boolean eligibleToKill = TaskKiller.update(status);
switch (response.result) { // SUPPRESS CHECKSTYLE MissingSwitchDefaultCheck
case UNKNOWN_TASK:
if (eligibleToKill) {
LOGGER.info("Received status update for unknown task, marking task to be killed: {}",
status.getTaskId().getValue());
TaskKiller.killTask(status.getTaskId());
} else {
// Special case: Mesos can send TASK_LOST+REASON_RECONCILIATION as a response to a
// prior kill request against a task that is unknown to Mesos. When this happens, we
// don't want to repeat the kill, because that would create a Kill -> Status -> Kill
// -> ... loop
LOGGER.warn(
"Received status update for unknown task, but task should not be killed again: {}",
status.getTaskId().getValue());
}
break;
case PROCESSED:
// No-op
break;
}
} catch (Throwable e) {
logExceptionAndExit(e);
}
}
private static P4Info getP4InfoMessage(URL p4InfoUrl) throws IOException {
InputStream p4InfoStream = p4InfoUrl.openStream();
P4Info.Builder p4InfoBuilder = P4Info.newBuilder();
TextFormat.getParser().merge(new InputStreamReader(p4InfoStream),
ExtensionRegistry.getEmptyRegistry(),
p4InfoBuilder);
return p4InfoBuilder.build();
}
@Memoized
MethodInvocations invocationReplacementConfigProto() {
try {
String protoText = Resources.toString(invocationReplacementConfigUrl(), UTF_8);
return TextFormat.parse(
protoText, ExtensionRegistry.getEmptyRegistry(), MethodInvocations.class);
} catch (IOException e) {
throw new IOError(e);
}
}
private Optional<Protos.TaskInfo> getTemplate(CassandraDaemonTask daemon) {
String templateTaskName = CassandraTemplateTask.toTemplateTaskName(daemon.getName());
try {
Optional<Protos.TaskInfo> info = getStateStore().fetchTask(templateTaskName);
LOGGER.info("Fetched template task for daemon '{}': {}",
daemon.getName(), TextFormat.shortDebugString(info.get()));
return info;
} catch (Exception e) {
LOGGER.warn(String.format(
"Failed to retrieve template task '%s'", templateTaskName), e);
return Optional.empty();
}
}
public static <K> String mapToString(
Map<K, ? extends Message> map) {
StringBuilder sb = new StringBuilder();
boolean first = true;
for (Map.Entry<K, ? extends Message> e : map.entrySet()) {
if (!first) {
sb.append("\n");
}
first = false;
sb.append(e.getKey()).append(": ")
.append(TextFormat.shortDebugString(e.getValue()));
}
return sb.toString();
}
@Override
public String toString() {
return TextFormat.shortDebugString(getProto());
}
@Override
public String toString() {
return TextFormat.shortDebugString(getProto());
}
public void format(DynamicMessage msg, Appendable output) throws IOException {
TextFormat.print(msg, output);
}
@Override //Object
public String toString() {
return TextFormat.shortDebugString(proto);
}
@Override
public String toString() {
return "EndpointAffinity [endpoint=" + TextFormat.shortDebugString(endpoint) + ", affinity=" + affinity +
", mandatory=" + mandatory + ", maxWidth=" + maxWidth + "]";
}
@Override
public String toString() {
return TextFormat.shortDebugString(getProto());
}
@Override
public String toString() {
return TextFormat.shortDebugString(getProto());
}
@Override
public String toString() {
return TextFormat.shortDebugString(getProto());
}
/**
* Runs single load test thread by calling gRPC Consensus service subscribeTopic endpoint. Success is dependant on
* StreamObserver observing the expected count for historic and incoming messages in the allotted time Returns
* SamplerResult to client
*
* @param messageListener listener properties
* @return Sampler result representing success and observed message counts
*/
@Override
public HCSSamplerResult subscribeTopic(MessageListener messageListener) {
log.info("Subscribing to topic using MAPI with {}, {}", () -> TextFormat
.shortDebugString(request), () -> messageListener);
CountDownLatch historicMessagesLatch = new CountDownLatch(messageListener.getHistoricMessagesCount());
CountDownLatch incomingMessagesLatch = new CountDownLatch(messageListener.getFutureMessagesCount());
TopicID topicId = request.getTopicID();
HCSMAPISamplerResult result = HCSMAPISamplerResult
.builder()
.realmNum(topicId.getRealmNum())
.topicNum(topicId.getTopicNum())
.success(true)
.build();
MirrorSubscriptionHandle subscription = null;
ScheduledExecutorService scheduler = null;
try {
subscription = mirrorConsensusTopicQuery
.subscribe(mirrorClient,
resp -> {
result.onNext(resp);
if (result.isHistorical()) {
historicMessagesLatch.countDown();
} else {
incomingMessagesLatch.countDown();
}
},
result::onError);
scheduler = Executors.newSingleThreadScheduledExecutor();
scheduler.scheduleAtFixedRate(() -> {
result.printProgress();
}, 0, 1, TimeUnit.MINUTES);
// await some new messages
if (!historicMessagesLatch.await(messageListener.getMessagesLatchWaitSeconds(), TimeUnit.SECONDS)) {
log.error("Historic messages latch count is {}, did not reach zero", historicMessagesLatch.getCount());
result.setSuccess(false);
}
if (historicMessagesLatch.getCount() == 0 && !incomingMessagesLatch
.await(messageListener.getMessagesLatchWaitSeconds(), TimeUnit.SECONDS)) {
log.error("incomingMessagesLatch count is {}, did not reach zero", incomingMessagesLatch.getCount());
result.setSuccess(false);
}
result.onComplete();
} catch (Exception ex) {
log.error("Error subscribing to topic", ex);
throw ex;
} finally {
if (subscription != null) {
subscription.unsubscribe();
log.info("Unsubscribed from {}", subscription);
}
scheduler.shutdownNow();
return result;
}
}
@Override
public String toString() {
return TextFormat.shortDebugString(getProto());
}