Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.accumulo.core.cli;

import java.time.ZoneOffset;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;

import com.google.gson.Gson;

/**
* A stable, versioned outer wrapper for all admin command JSON output.
*
* <p>
* Every command that supports --json output wraps its command-specific data in this envelope. This
* provides a consistent structure that scripts can rely on regardless of which command produced the
* output:
*
* <pre>
* {
* "command": "accumulo admin fate --summary",
* "version": "1",
* "reportTime": "2026-06-04T12:00:00Z",
* "status": "OK",
* "message": null,
* "data": { ...command-specific payload... }

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if the command output should be it's own top level element instead. For example,

{
  "status": {
    "command": "accumulo admin fate --summary",
    "version": "1",
    "reportTime": "2026-06-04T12:00:00Z",
    "status": "OK",
    "message": ""
  },
  "output": {
  }
}

* }
* </pre>
*
* <p>
* The {@link version} field is a stability contract. When a breaking change is made to the envelope
* structure, the version will be incremented. Scripts should check this field and handle the
* version they were written against.
*
*/
public class CommandOutputEnvelope {

/**
* Current envelop schema version. Increment this if a breaking structural change is made to the
* envelope fields (not to the {@link data} field, data changes command specific).
*/
public static final String VERSION = "1.0";
private static final DateTimeFormatter ISO_FMT =
DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss'Z'");
Comment on lines +59 to +60

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if it would be more user friendly to use the hosts timezone.

private static final Gson PRETTY_GSON =
new Gson().newBuilder().setPrettyPrinting().disableJdkUnsafe().create();

private String command;
private String version;
private String reportTime;
private String status;
private String message;
private Object data;

@SuppressWarnings("unused")
private CommandOutputEnvelope() {}

private CommandOutputEnvelope(String command, String status, String message, Object data) {
this.command = command;
this.version = VERSION;
this.reportTime = ISO_FMT.format(ZonedDateTime.now(ZoneOffset.UTC));
this.status = status;
this.message = message;
Comment on lines +78 to +79

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Might be able to combine these two fields into one. For success the value could be OK. For a failure the value could be ERROR: <message>

this.data = data;
}

public static CommandOutputEnvelope of(String command, Object data) {
return new CommandOutputEnvelope(command, "OK", null, data);
}

public static CommandOutputEnvelope error(String command, String message) {
return new CommandOutputEnvelope(command, "ERROR", message, null);
}

public String toJson() {
return PRETTY_GSON.toJson(this);
}

public static CommandOutputEnvelope fromJson(String json) {
return PRETTY_GSON.fromJson(json, CommandOutputEnvelope.class);
}

public String getCommand() {
return command;
}

public String getVersion() {
return version;
}

public String getReportTime() {
return reportTime;
}

public String getStatus() {
return status;
}

public String getMessage() {
return message;
}

public Object getData() {
return data;
}
}
51 changes: 51 additions & 0 deletions core/src/main/java/org/apache/accumulo/core/cli/CommandReport.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.accumulo.core.cli;

import java.util.List;

/**
* Implemented by all command report classes that support both human-readable and computer readable
* outputs.
* <p>
* Json output is always wrapped in a {@link CommandOutputEnvelope} to provide a stable, versioned
* outer structure that scripts can depend on, regardless of which command is used.
*
* <p>
* Usage pattern is a command's execute() method:
*
* <pre>
* CommandReport report = buildReport(context, options);
* if (options.json()) {
* System.out.println(report.toEnvelopedJson("accumulo admin 'my-command'"));
* } else {
* report.formatLines().forEach(System.out::println);
* }
* </pre>
*/
public interface CommandReport {
List<String> formatLines();

Object getData();

default String toEnvelopedJson(String commandName) {
return CommandOutputEnvelope.of(commandName, getData()).toJson();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@ public List<String> split(String value) {
+ " Expected format: -o <key>=<value> [-o <key>=<value>]")
private List<String> overrides = new ArrayList<>();

@Parameter(names = {"-j", "--json"},
description = "Print output in JSON format. Output is wrapped in standard envelope with command, version, reportTime, status and data fields.")
public boolean json = false;

private SiteConfiguration siteConfig = null;

public synchronized SiteConfiguration getSiteConfiguration() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
package org.apache.accumulo.server.util;

import org.apache.accumulo.core.cli.BaseKeywordExecutable;
import org.apache.accumulo.core.cli.CommandOutputEnvelope;
import org.apache.accumulo.core.cli.ServerOpts;
import org.apache.accumulo.core.conf.AccumuloConfiguration;
import org.apache.accumulo.core.conf.Property;
Expand Down Expand Up @@ -58,6 +59,13 @@ public void doExecute(JCommander cl, OPTS options) throws Exception {
SecurityUtil.serverLogin(conf);
}
execute(cl, options);
} catch (Exception e) {
if (options.json) {
String commandName = "accumulo"
+ (commandGroup().key().isBlank() ? "" : " " + commandGroup().key()) + " " + keyword();
System.out.println(CommandOutputEnvelope.error(commandName, e.getMessage()).toJson());
}
throw e;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -137,10 +137,6 @@ static class FateOpts extends ServerOpts {
description = "[<FateId>...] Print a summary of FaTE transactions. Print only the FateId's specified or print all transactions if empty. Use -s to only print those with certain states. Use -t to only print those with certain FateInstanceTypes. Use -j to print the transactions in json.")
boolean summarize;

@Parameter(names = {"-j", "--json"},
description = "Print transactions in json. Only useful for --summary command.")
boolean printJson;

@Parameter(names = {"-s", "--state"},
description = "<state>... Print transactions in the state(s) {NEW, IN_PROGRESS, FAILED_IN_PROGRESS, FAILED, SUCCESSFUL}")
List<String> states = new ArrayList<>();
Expand Down Expand Up @@ -383,8 +379,9 @@ private void summarizeFateTx(ServerContext context, FateOpts cmd, AdminUtil<Fate

// gather statistics
transactions.getTransactions().forEach(report::gatherTxnStatus);
if (cmd.printJson) {
printLines(Collections.singletonList(report.toJson()));
if (cmd.json) {
printLines(
Collections.singletonList(report.toEnvelopedJson("accumulo admin fate --summary")));
} else {
printLines(report.formatLines());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,6 @@ public class ServiceStatus extends ServerKeywordExecutable<ServiceStatusCmdOpts>

static class ServiceStatusCmdOpts extends ServerOpts {

@Parameter(names = "--json", description = "provide output in json format")
boolean json = false;

@Parameter(names = "--showHosts",
description = "provide a summary of service counts with host details")
boolean showHosts = false;
Expand Down Expand Up @@ -106,17 +103,15 @@ public void execute(JCommander cl, ServiceStatusCmdOpts options) throws Exceptio
ServiceStatusReport report = new ServiceStatusReport(services, options.showHosts);

if (options.json) {
System.out.println(report.toJson());
System.out.println(report.toEnvelopedJson("accumulo admin service-status"));

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It might make sense to add a method to ServerKeywordExecutable that returns the invoked command. This would make this more flexible if the command group or command name changes in the future.

} else {
StringBuilder sb = new StringBuilder(8192);
report.report(sb);
System.out.println(sb);
report.formatLines().forEach(System.out::println);
}
}

/**
* The manager paths in ZooKeeper are: {@code /accumulo/[IID]/managers/lock/zlock#[NUM]} with the
* lock data providing a service descriptor with host and port.
* op The manager paths in ZooKeeper are: {@code /accumulo/[IID]/managers/lock/zlock#[NUM]} with
* the lock data providing a service descriptor with host and port.
*/
@VisibleForTesting
StatusSummary getManagerStatus(ServerContext context) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import java.util.TreeMap;
import java.util.TreeSet;

import org.apache.accumulo.core.cli.CommandReport;
import org.apache.accumulo.core.fate.AdminUtil;
import org.apache.accumulo.core.fate.Fate;
import org.apache.accumulo.core.fate.FateId;
Expand All @@ -41,7 +42,7 @@
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

public class FateSummaryReport {
public class FateSummaryReport implements CommandReport {

private Map<String,Integer> statusCounts = new TreeMap<>();
private Map<String,Integer> cmdCounts = new TreeMap<>();
Expand Down Expand Up @@ -154,6 +155,7 @@ public static FateSummaryReport fromJson(final String jsonString) {
*
* @return formatted report lines.
*/
@Override
public List<String> formatLines() {
List<String> lines = new ArrayList<>();

Expand Down Expand Up @@ -185,4 +187,9 @@ public List<String> formatLines() {

return lines;
}

@Override
public Object getData() {
return this;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,12 @@
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.apache.accumulo.core.cli.CommandReport;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand All @@ -39,7 +42,19 @@
/**
* Wrapper for JSON formatted report.
*/
public class ServiceStatusReport {
public class ServiceStatusReport implements CommandReport {

@Override
public List<String> formatLines() {
StringBuilder sb = new StringBuilder(8192);
report(sb);
return Arrays.asList(sb.toString().split("\n"));
}

@Override
public Object getData() {
return this;
}

private static class HostExclusionStrategy implements ExclusionStrategy {

Expand Down
Loading