Skip to content

Commit 88ec4ad

Browse files
committed
Harmonize and enhance Javadoc comments across Elasticsearch codebase
- Reviewed and updated Javadoc for 145 Java files across all major modules - Enhanced 800+ public methods with comprehensive documentation - Added 200+ practical usage examples using standard template format - Harmonized documentation for similar methods across the codebase - Ensured all public methods have complete @param, @return, @throws tags - Updated class-level documentation with clear purpose and usage guidance Coverage: - Server module: action, cluster, index, search, common, and other packages - Libs: CLI, core, dissect, geo, grok modules - Modules: health-shards-availability, ingest-attachment, ingest-otel, kibana, runtime-fields-common, systemd - Plugins: analysis (ICU, Kuromoji, Nori, Phonetic, SmartCN, Stempel, Ukrainian), discovery-ec2, mapper-size, store-smb - X-Pack: core (license), esql, ml, async, frozen-indices, graph, mappers, vector-tile, shutdown, and other plugins Key improvements: - Consistent documentation structure and terminology - Accurate descriptions reflecting actual implementation - Enhanced clarity with proper parameter and return value explanations - Added usage examples demonstrating real-world patterns - Cross-references to related classes using @link and @see tags All changes maintain valid Javadoc syntax and follow Elasticsearch documentation standards.
1 parent c9c50ea commit 88ec4ad

File tree

145 files changed

+8609
-419
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

145 files changed

+8609
-419
lines changed

libs/cli/src/main/java/org/elasticsearch/cli/Command.java

Lines changed: 105 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -40,15 +40,44 @@ public abstract class Command implements Closeable {
4040
.availableUnless(silentOption);
4141

4242
/**
43-
* Construct the command with the specified command description and runnable to execute before main is invoked.
44-
* @param description the command description
43+
* Constructs the command with the specified command description.
4544
*
45+
* @param description the command description to be displayed in help output
46+
*
47+
* <p><b>Usage Example:</b></p>
48+
* <pre>{@code
49+
* public class MyCommand extends Command {
50+
* public MyCommand() {
51+
* super("Performs custom processing");
52+
* }
53+
* }
54+
* }</pre>
4655
*/
4756
public Command(final String description) {
4857
this.description = description;
4958
}
5059

51-
/** Parses options for this command from args and executes it. */
60+
/**
61+
* Parses command-line options and executes this command with proper error handling.
62+
*
63+
* <p>This is the main entry point for command execution. It handles parsing
64+
* of command-line arguments, error handling, and returning appropriate exit codes.
65+
* All exceptions are caught and converted to appropriate exit codes.
66+
*
67+
* @param args the command-line arguments to parse
68+
* @param terminal the terminal for input/output operations
69+
* @param processInfo information about the current process (system properties, environment variables, etc.)
70+
* @return the exit code (0 for success, non-zero for errors as defined in {@link ExitCodes})
71+
* @throws IOException if an I/O error occurs during command execution
72+
*
73+
* <p><b>Usage Example:</b></p>
74+
* <pre>{@code
75+
* Command cmd = new MyCommand();
76+
* Terminal terminal = Terminal.DEFAULT;
77+
* ProcessInfo processInfo = ProcessInfo.fromSystem();
78+
* int exitCode = cmd.main(new String[]{"--verbose"}, terminal, processInfo);
79+
* }</pre>
80+
*/
5281
public final int main(String[] args, Terminal terminal, ProcessInfo processInfo) throws IOException {
5382
try {
5483
mainWithoutErrorHandling(args, terminal, processInfo);
@@ -76,7 +105,16 @@ public final int main(String[] args, Terminal terminal, ProcessInfo processInfo)
76105
}
77106

78107
/**
79-
* Executes the command, but all errors are thrown.
108+
* Executes the command without error handling, allowing all exceptions to propagate.
109+
*
110+
* <p>This method parses options, handles help and verbosity flags, and delegates
111+
* to {@link #execute(Terminal, OptionSet, ProcessInfo)}. Unlike {@link #main(String[], Terminal, ProcessInfo)},
112+
* this method does not catch exceptions, allowing callers to handle them.
113+
*
114+
* @param args the command-line arguments to parse
115+
* @param terminal the terminal for input/output operations
116+
* @param processInfo information about the current process
117+
* @throws Exception if any error occurs during command execution
80118
*/
81119
protected void mainWithoutErrorHandling(String[] args, Terminal terminal, ProcessInfo processInfo) throws Exception {
82120
final OptionSet options = parseOptions(args);
@@ -102,9 +140,16 @@ protected void mainWithoutErrorHandling(String[] args, Terminal terminal, Proces
102140
}
103141

104142
/**
105-
* Parse command line arguments for this command.
106-
* @param args The string arguments passed to the command
107-
* @return A set of parsed options
143+
* Parses command-line arguments for this command using the configured option parser.
144+
*
145+
* @param args the string arguments passed to the command
146+
* @return a set of parsed options
147+
* @throws joptsimple.OptionException if the arguments cannot be parsed
148+
*
149+
* <p><b>Usage Example:</b></p>
150+
* <pre>{@code
151+
* OptionSet options = parseOptions(new String[]{"--verbose", "input.txt"});
152+
* }</pre>
108153
*/
109154
public OptionSet parseOptions(String[] args) {
110155
return parser.parse(args);
@@ -126,27 +171,77 @@ private void printHelp(Terminal terminal, boolean toStdError) throws IOException
126171
}
127172
}
128173

129-
/** Prints additional help information, specific to the command */
174+
/**
175+
* Prints additional help information specific to this command.
176+
*
177+
* <p>Subclasses can override this method to provide command-specific help text
178+
* that will be displayed when the user requests help via the -h or --help option.
179+
*
180+
* @param terminal the terminal to write help output to
181+
*/
130182
protected void printAdditionalHelp(Terminal terminal) {}
131183

184+
/**
185+
* Prints a user exception message to the terminal's error stream.
186+
*
187+
* <p>Subclasses can override this method to customize how user exceptions are displayed.
188+
*
189+
* @param terminal the terminal to write error output to
190+
* @param e the user exception to print
191+
*/
132192
protected void printUserException(Terminal terminal, UserException e) {
133193
if (e.getMessage() != null) {
134194
terminal.errorPrintln("");
135195
terminal.errorPrintln(Terminal.Verbosity.SILENT, "ERROR: " + e.getMessage() + ", with exit code " + e.exitCode);
136196
}
137197
}
138198

199+
/**
200+
* Exits the JVM with the specified status code.
201+
*
202+
* <p>This method calls {@link System#exit(int)} and should be used sparingly,
203+
* typically only after {@link #main(String[], Terminal, ProcessInfo)} has completed.
204+
*
205+
* @param status the exit status code (0 for success, non-zero for errors)
206+
*/
139207
@SuppressForbidden(reason = "Allowed to exit explicitly from #main()")
140208
protected static void exit(int status) {
141209
System.exit(status);
142210
}
143211

144212
/**
145-
* Executes this command.
213+
* Executes the core logic of this command.
146214
*
147-
* Any runtime user errors (like an input file that does not exist), should throw a {@link UserException}. */
215+
* <p>Subclasses must implement this method to provide command-specific functionality.
216+
* This method is called by {@link #mainWithoutErrorHandling(String[], Terminal, ProcessInfo)}
217+
* after options have been parsed and help/verbosity flags processed.
218+
*
219+
* @param terminal the terminal for input/output operations
220+
* @param options the parsed command-line options
221+
* @param processInfo information about the current process
222+
* @throws Exception if any error occurs during execution
223+
* @throws UserException for user-correctable errors (e.g., invalid input file)
224+
*
225+
* <p><b>Usage Example:</b></p>
226+
* <pre>{@code
227+
* @Override
228+
* protected void execute(Terminal terminal, OptionSet options, ProcessInfo processInfo) throws Exception {
229+
* String input = options.valueOf(inputOption);
230+
* terminal.println("Processing: " + input);
231+
* // ... perform command logic ...
232+
* }
233+
* }</pre>
234+
*/
148235
protected abstract void execute(Terminal terminal, OptionSet options, ProcessInfo processInfo) throws Exception;
149236

237+
/**
238+
* Closes this command and releases any resources.
239+
*
240+
* <p>The default implementation does nothing. Subclasses should override this method
241+
* to release any resources they have acquired.
242+
*
243+
* @throws IOException if an I/O error occurs while closing resources
244+
*/
150245
@Override
151246
public void close() throws IOException {
152247

libs/cli/src/main/java/org/elasticsearch/cli/ExitCodes.java

Lines changed: 51 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -10,26 +10,60 @@
1010
package org.elasticsearch.cli;
1111

1212
/**
13-
* POSIX exit codes.
13+
* Standard POSIX exit codes for command-line tools.
14+
*
15+
* <p>These exit codes follow POSIX conventions and are used by CLI commands
16+
* to indicate the result of their execution. These values are part of the public
17+
* API and may be used in scripts, so they should not be changed.
18+
*
19+
* <p><b>Warning:</b> Do not modify these values as they may be used in external scripts
20+
* where usages are not tracked by the IDE.
1421
*/
1522
public class ExitCodes {
16-
// please be extra careful when changing these as the values might be used in scripts,
17-
// usages of which are not tracked by the IDE
23+
/** Successful completion (exit code 0). */
1824
public static final int OK = 0;
19-
public static final int USAGE = 64; // command line usage error
20-
public static final int DATA_ERROR = 65; // data format error
21-
public static final int NO_INPUT = 66; // cannot open input
22-
public static final int NO_USER = 67; // addressee unknown
23-
public static final int NO_HOST = 68; // host name unknown
24-
public static final int UNAVAILABLE = 69; // service unavailable
25-
public static final int CODE_ERROR = 70; // internal software error
26-
public static final int CANT_CREATE = 73; // can't create (user) output file
27-
public static final int IO_ERROR = 74; // input/output error
28-
public static final int TEMP_FAILURE = 75; // temp failure; user is invited to retry
29-
public static final int PROTOCOL = 76; // remote error in protocol
30-
public static final int NOPERM = 77; // permission denied
31-
public static final int CONFIG = 78; // configuration error
32-
public static final int NOOP = 80; // nothing to do
25+
26+
/** Command line usage error (exit code 64). */
27+
public static final int USAGE = 64;
28+
29+
/** Data format error (exit code 65). */
30+
public static final int DATA_ERROR = 65;
31+
32+
/** Cannot open input (exit code 66). */
33+
public static final int NO_INPUT = 66;
34+
35+
/** Addressee unknown (exit code 67). */
36+
public static final int NO_USER = 67;
37+
38+
/** Host name unknown (exit code 68). */
39+
public static final int NO_HOST = 68;
40+
41+
/** Service unavailable (exit code 69). */
42+
public static final int UNAVAILABLE = 69;
43+
44+
/** Internal software error (exit code 70). */
45+
public static final int CODE_ERROR = 70;
46+
47+
/** Can't create (user) output file (exit code 73). */
48+
public static final int CANT_CREATE = 73;
49+
50+
/** Input/output error (exit code 74). */
51+
public static final int IO_ERROR = 74;
52+
53+
/** Temporary failure; user is invited to retry (exit code 75). */
54+
public static final int TEMP_FAILURE = 75;
55+
56+
/** Remote error in protocol (exit code 76). */
57+
public static final int PROTOCOL = 76;
58+
59+
/** Permission denied (exit code 77). */
60+
public static final int NOPERM = 77;
61+
62+
/** Configuration error (exit code 78). */
63+
public static final int CONFIG = 78;
64+
65+
/** Nothing to do (exit code 80). */
66+
public static final int NOOP = 80;
3367

3468
private ExitCodes() { /* no instance, just constants */ }
3569
}

libs/cli/src/main/java/org/elasticsearch/cli/MultiCommand.java

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,20 @@ public class MultiCommand extends Command {
3434
private final OptionSpec<KeyValuePair> settingOption;
3535

3636
/**
37-
* Construct the multi-command with the specified command description and runnable to execute before main is invoked.
38-
* @param description the multi-command description
37+
* Constructs a multi-command with the specified description.
3938
*
39+
* <p>A MultiCommand is a CLI tool that contains multiple sub-commands, each
40+
* represented by a separate {@link Command} instance. The user specifies which
41+
* sub-command to run as the first argument.
42+
*
43+
* @param description the multi-command description to be displayed in help output
44+
*
45+
* <p><b>Usage Example:</b></p>
46+
* <pre>{@code
47+
* MultiCommand tool = new MultiCommand("Elasticsearch administration tool");
48+
* tool.subcommands.put("index", new IndexCommand());
49+
* tool.subcommands.put("cluster", new ClusterCommand());
50+
* }</pre>
4051
*/
4152
public MultiCommand(final String description) {
4253
super(description);
@@ -70,6 +81,21 @@ private void printSubCommandList(Consumer<String> println) {
7081
println.accept("");
7182
}
7283

84+
/**
85+
* Executes the appropriate sub-command based on the first command-line argument.
86+
*
87+
* <p>This method parses the first non-option argument to determine which sub-command
88+
* to execute, then delegates to that sub-command's {@link Command#mainWithoutErrorHandling(String[], Terminal, ProcessInfo)}
89+
* method.
90+
*
91+
* @param terminal the terminal for input/output operations
92+
* @param options the parsed command-line options
93+
* @param processInfo information about the current process
94+
* @throws Exception if an error occurs during sub-command execution
95+
* @throws MissingCommandException if no sub-command name is provided
96+
* @throws UserException if the specified sub-command does not exist
97+
* @throws IllegalStateException if no sub-commands have been configured
98+
*/
7399
@Override
74100
protected void execute(Terminal terminal, OptionSet options, ProcessInfo processInfo) throws Exception {
75101
if (subcommands.isEmpty()) {
@@ -95,6 +121,14 @@ protected void execute(Terminal terminal, OptionSet options, ProcessInfo process
95121
subcommand.mainWithoutErrorHandling(args.toArray(new String[0]), terminal, processInfo);
96122
}
97123

124+
/**
125+
* Closes this multi-command and all of its sub-commands.
126+
*
127+
* <p>This method iterates through all registered sub-commands and closes each one,
128+
* ensuring proper resource cleanup.
129+
*
130+
* @throws IOException if an I/O error occurs while closing any sub-command
131+
*/
98132
@Override
99133
public void close() throws IOException {
100134
IOUtils.close(subcommands.values());

0 commit comments

Comments
 (0)