Skip to content
Closed
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
Expand Up @@ -22,6 +22,7 @@
import java.util.concurrent.Callable;

import org.apache.hadoop.hdds.conf.OzoneConfiguration;
import org.apache.hadoop.fs.Path;

import com.google.common.annotations.VisibleForTesting;
import picocli.CommandLine;
Expand All @@ -41,6 +42,9 @@ public class GenericCli implements Callable<Void>, GenericParentCommand {
@Option(names = {"-D", "--set"})
private Map<String, String> configurationOverrides = new HashMap<>();

@Option(names = {"-conf"})
private String configurationPath;

private final CommandLine cmd;

public GenericCli() {
Expand Down Expand Up @@ -70,19 +74,19 @@ protected void printError(Throwable error) {
} else {
System.err.println(error.getMessage().split("\n")[0]);
}
if(error instanceof MissingSubcommandException){
System.err.println(((MissingSubcommandException) error).getUsage());
}
}

@Override
public Void call() throws Exception {
throw new MissingSubcommandException(cmd.getUsageMessage());
Copy link
Member

Choose a reason for hiding this comment

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

LIKE. Thanks to fix this part (and the similar items). It's better to use the included CommandLine.ParameterException...

throw new MissingSubcommandException(cmd);
}

@Override
public OzoneConfiguration createOzoneConfiguration() {
OzoneConfiguration ozoneConf = new OzoneConfiguration();
if (configurationPath != null) {
ozoneConf.addResource(new Path(configurationPath));
}
if (configurationOverrides != null) {
for (Entry<String, String> entry : configurationOverrides.entrySet()) {
ozoneConf.set(entry.getKey(), entry.getValue());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,15 @@
*/
package org.apache.hadoop.hdds.cli;

import picocli.CommandLine;

/**
* Exception to throw if subcommand is not selected but required.
*/
public class MissingSubcommandException extends RuntimeException {

private String usage;
public class MissingSubcommandException extends CommandLine.ParameterException {

public MissingSubcommandException(String usage) {
super("Incomplete command");
this.usage = usage;
public MissingSubcommandException(CommandLine cmd) {
super(cmd, "Incomplete command");
}

public String getUsage() {
return usage;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
import java.net.InetAddress;
import java.security.KeyPair;
import java.security.cert.CertificateException;
import java.util.Arrays;
import java.util.List;
import java.util.UUID;

Expand Down Expand Up @@ -81,64 +82,46 @@ public class HddsDatanodeService extends GenericCli implements ServicePlugin {
private CertificateClient dnCertClient;
private String component;
private HddsDatanodeHttpServer httpServer;
private boolean printBanner;
private String[] args;

/**
* Default constructor.
*/
public HddsDatanodeService() {
this(null);
public HddsDatanodeService(boolean printBanner, String[] args) {
this.printBanner = printBanner;
this.args = args != null ? Arrays.copyOf(args, args.length) : null;
}

/**
* Constructs {@link HddsDatanodeService} using the provided {@code conf}
* value.
* Create an Datanode instance based on the supplied command-line arguments.
* <p>
* This method is intended for unit tests only. It suppresses the
* startup/shutdown message and skips registering Unix signal handlers.
*
* @param conf OzoneConfiguration
* @param args command line arguments.
* @return Datanode instance
*/
public HddsDatanodeService(Configuration conf) {
if (conf == null) {
this.conf = new OzoneConfiguration();
} else {
this.conf = new OzoneConfiguration(conf);
}
}

@VisibleForTesting
public static HddsDatanodeService createHddsDatanodeService(
String[] args, Configuration conf) {
return createHddsDatanodeService(args, conf, false);
String[] args) {
return createHddsDatanodeService(args, false);
}

/**
* Create an Datanode instance based on the supplied command-line arguments.
* <p>
* This method is intended for unit tests only. It suppresses the
* startup/shutdown message and skips registering Unix signal handlers.
*
* @param args command line arguments.
* @param conf HDDS configuration
* @param printBanner if true, then log a verbose startup message.
* @return Datanode instance
*/
private static HddsDatanodeService createHddsDatanodeService(
String[] args, Configuration conf, boolean printBanner) {
if (args.length == 0 && printBanner) {
StringUtils
.startupShutdownMessage(HddsDatanodeService.class, args, LOG);

}
return new HddsDatanodeService(conf);
String[] args, boolean printBanner) {
return new HddsDatanodeService(printBanner, args);
}

public static void main(String[] args) {
try {
Configuration conf = new OzoneConfiguration();
HddsDatanodeService hddsDatanodeService =
createHddsDatanodeService(args, conf, true);
if (hddsDatanodeService != null) {
hddsDatanodeService.start(null);
hddsDatanodeService.join();
}
createHddsDatanodeService(args, true);
hddsDatanodeService.run(args);
} catch (Throwable e) {
LOG.error("Exception in HddsDatanodeService.", e);
terminate(1, e);
Expand All @@ -149,19 +132,43 @@ public static Logger getLogger() {
return LOG;
}

@Override
public Void call() throws Exception {
if (printBanner) {
StringUtils
.startupShutdownMessage(HddsDatanodeService.class, args, LOG);
}
start(createOzoneConfiguration());
join();
return null;
}

public void setConfiguration(OzoneConfiguration configuration) {
this.conf = configuration;
}

/**
* Starts HddsDatanode services.
*
* @param service The service instance invoking this method
*/
@Override
public void start(Object service) {
if (service instanceof Configurable) {
start(new OzoneConfiguration(((Configurable) service).getConf()));
} else {
Copy link
Member

Choose a reason for hiding this comment

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

I think this part can be simplified (but fix me if I am wrong) with using two start method:

 /**
   * Starts HddsDatanode services.
   *
   * @param service The service instance invoking this method
   */
  @Override
  public void start(Object service) {
    if (service instanceof Configurable) {
      start(new OzoneConfiguration(((Configurable) service).getConf()));
    } else {
      start(new OzoneConfiguration());
    }
  }

  public void start(OzoneConfiguration conf) {
    this.conf = conf;
    DefaultMetricsSystem.initialize("HddsDatanode");
    OzoneConfiguration.activate();
    if (HddsUtils.isHddsEnabled(conf)) {

In this case we don't need to add the additional method to the GenericParentCommand so it can be as simple as now.

And from the new call() method we can use the createOzoneConfiguration()

  @Override
  public Void call() throws Exception {
    if (printBanner) {
      StringUtils
          .startupShutdownMessage(HddsDatanodeService.class, args, LOG);
    }
    start(createOzoneConfiguration());
    join();
    return null;
  }

initConf also can be removed with this approach (IMHO)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is a good idea, I simplified this part, but I introduced a setConfiguration method here as well so that I don't have to modify MiniOzoneCluster that much.

start(new OzoneConfiguration());
}
}

public void start(OzoneConfiguration configuration) {
setConfiguration(configuration);
start();
}

public void start() {
DefaultMetricsSystem.initialize("HddsDatanode");
OzoneConfiguration.activate();
if (service instanceof Configurable) {
conf = new OzoneConfiguration(((Configurable) service).getConf());
}
if (HddsUtils.isHddsEnabled(conf)) {
try {
String hostname = HddsUtils.getHostName(conf);
Expand Down Expand Up @@ -404,11 +411,13 @@ public DatanodeStateMachine getDatanodeStateMachine() {
}

public void join() {
try {
datanodeStateMachine.join();
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
LOG.info("Interrupted during StorageContainerManager join.");
if (datanodeStateMachine != null) {
try {
datanodeStateMachine.join();
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
LOG.info("Interrupted during StorageContainerManager join.");
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
*/
public class TestHddsDatanodeService {
private File testDir;
private Configuration conf;
private OzoneConfiguration conf;
private HddsDatanodeService service;
private String[] args = new String[] {};

Expand All @@ -64,8 +64,8 @@ public void tearDown() {

@Test
public void testStartup() throws IOException {
service = HddsDatanodeService.createHddsDatanodeService(args, conf);
service.start(null);
service = HddsDatanodeService.createHddsDatanodeService(args);
service.start(conf);
service.join();

assertNotNull(service.getDatanodeDetails());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,10 +83,10 @@ public static void setUp() throws Exception {
ServicePlugin.class);
securityConfig = new SecurityConfig(conf);

service = HddsDatanodeService.createHddsDatanodeService(args, conf);
service = HddsDatanodeService.createHddsDatanodeService(args);
dnLogs = GenericTestUtils.LogCapturer.captureLogs(getLogger());
callQuietly(() -> {
service.start(null);
service.start(conf);
return null;
});
callQuietly(() -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@ public SCMCLI getParent() {
@Override
public Void call() throws Exception {
throw new MissingSubcommandException(
this.parent.getCmd().getSubcommands().get("safemode").
getUsageMessage());
this.parent.getCmd().getSubcommands().get("safemode"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
import java.util.Optional;
import org.apache.commons.io.FileUtils;
import org.apache.hadoop.classification.InterfaceAudience;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.conf.StorageUnit;
import org.apache.hadoop.hdds.HddsConfigKeys;
import org.apache.hadoop.hdds.protocol.DatanodeDetails;
Expand Down Expand Up @@ -104,9 +103,9 @@ public class MiniOzoneClusterImpl implements MiniOzoneCluster {
* @throws IOException if there is an I/O error
*/
MiniOzoneClusterImpl(OzoneConfiguration conf,
OzoneManager ozoneManager,
StorageContainerManager scm,
List<HddsDatanodeService> hddsDatanodes) {
OzoneManager ozoneManager,
StorageContainerManager scm,
List<HddsDatanodeService> hddsDatanodes) {
this.conf = conf;
this.ozoneManager = ozoneManager;
this.scm = scm;
Expand Down Expand Up @@ -275,7 +274,7 @@ public void restartHddsDatanode(int i, boolean waitForDatanode)
datanodeService.stop();
datanodeService.join();
// ensure same ports are used across restarts.
Configuration config = datanodeService.getConf();
OzoneConfiguration config = datanodeService.getConf();
int currentPort = datanodeService.getDatanodeDetails()
.getPort(DatanodeDetails.Port.Name.STANDALONE).getValue();
config.setInt(DFS_CONTAINER_IPC_PORT, currentPort);
Expand All @@ -291,9 +290,9 @@ public void restartHddsDatanode(int i, boolean waitForDatanode)
}
String[] args = new String[]{};
HddsDatanodeService service =
HddsDatanodeService.createHddsDatanodeService(args, config);
HddsDatanodeService.createHddsDatanodeService(args);
hddsDatanodes.add(i, service);
service.start(null);
service.start(config);
if (waitForDatanode) {
// wait for the node to be identified as a healthy node again.
waitForClusterToBeReady();
Expand Down Expand Up @@ -371,7 +370,7 @@ public void startScm() throws IOException {
public void startHddsDatanodes() {
hddsDatanodes.forEach((datanode) -> {
datanode.setCertificateClient(getCAClient());
datanode.start(null);
datanode.start();
});
}

Expand Down Expand Up @@ -537,7 +536,7 @@ List<HddsDatanodeService> createHddsDatanodes(
conf.setStrings(ScmConfigKeys.OZONE_SCM_NAMES, scmAddress);
List<HddsDatanodeService> hddsDatanodes = new ArrayList<>();
for (int i = 0; i < numOfDatanodes; i++) {
Configuration dnConf = new OzoneConfiguration(conf);
OzoneConfiguration dnConf = new OzoneConfiguration(conf);
String datanodeBaseDir = path + "/datanode-" + Integer.toString(i);
Path metaDir = Paths.get(datanodeBaseDir, "meta");
Path dataDir = Paths.get(datanodeBaseDir, "data", "containers");
Expand All @@ -555,8 +554,10 @@ List<HddsDatanodeService> createHddsDatanodes(
dnConf.set(OzoneConfigKeys.OZONE_CONTAINER_COPY_WORKDIR,
wrokDir.toString());

hddsDatanodes.add(
HddsDatanodeService.createHddsDatanodeService(args, dnConf));
HddsDatanodeService datanode
= HddsDatanodeService.createHddsDatanodeService(args);
datanode.setConfiguration(dnConf);
hddsDatanodes.add(datanode);
}
return hddsDatanodes;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,7 @@ public void testSCMSafeMode() throws Exception {
assertFalse(logCapturer.getOutput().contains("SCM exiting safe mode."));
assertTrue(scm.getCurrentContainerThreshold() == 0);
for (HddsDatanodeService dn : cluster.getHddsDatanodes()) {
dn.start(null);
dn.start();
}
GenericTestUtils
.waitFor(() -> scm.getCurrentContainerThreshold() == 1.0, 100, 20000);
Expand Down
Loading