Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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,8 +22,11 @@
import org.apache.hadoop.hdds.cli.GenericCli;
import org.apache.hadoop.hdds.cli.HddsVersionProvider;
import org.apache.hadoop.hdds.conf.OzoneConfiguration;
import org.apache.hadoop.hdds.tracing.TracingUtil;
import picocli.CommandLine;

import static org.apache.hadoop.fs.ozone.Constants.OZONE_DEFAULT_USER;
Comment thread
DaveTeng0 marked this conversation as resolved.
Outdated

/**
* Ozone Repair Command line tool.
*/
Expand All @@ -33,6 +36,13 @@
mixinStandardHelpOptions = true)
public class OzoneRepair extends GenericCli {

public static final String WARNING_SYS_USER_MESSAGE =
"ATTENTION: You are currently logged in as user '%s'. Ozone typically runs as user '%s'." +
" If you proceed with this command, it may change the ownership of RocksDB files " +
" used by the Ozone Manager (OM). This ownership change could prevent OM from starting successfully." +
" Are you sure you want to continue (y/N)? ";


private OzoneConfiguration ozoneConf;

public OzoneRepair() {
Expand Down Expand Up @@ -61,4 +71,33 @@ public OzoneConfiguration getOzoneConf() {
public static void main(String[] argv) throws Exception {
new OzoneRepair().run(argv);
}

@Override
public int execute(String[] argv) {
String currentUser = getSystemUserName();
boolean shouldProceed = true;
if (!currentUser.equals(OZONE_DEFAULT_USER)) {
String s = getConsoleReadLineWithFormat(currentUser, OZONE_DEFAULT_USER);
shouldProceed = Boolean.parseBoolean(s) || "y".equalsIgnoreCase(s);
}
if (!shouldProceed) {
System.out.println("Aborting command.");
return 1;
}
System.out.println("Run as user: " + currentUser);

TracingUtil.initTracing("shell", createOzoneConfiguration());
String spanName = "ozone repair " + String.join(" ", argv);
return TracingUtil.executeInNewSpan(spanName,
() -> super.execute(argv));
Comment thread
DaveTeng0 marked this conversation as resolved.
Outdated
}

public String getSystemUserName() {
return System.getProperty("user.name");
}

public String getConsoleReadLineWithFormat(String currentUser, String defaultUser) {
return System.console().readLine(String.format(WARNING_SYS_USER_MESSAGE, currentUser, defaultUser));
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
/*
* 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
*
* http://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.hadoop.ozone.repair;

import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import java.io.ByteArrayOutputStream;
import java.io.PrintStream;

import static java.nio.charset.StandardCharsets.UTF_8;
import static org.apache.hadoop.fs.ozone.Constants.OZONE_DEFAULT_USER;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.spy;

/**
* Tests the ozone repair command.
*/
public class TestOzoneRepair {

private final ByteArrayOutputStream out = new ByteArrayOutputStream();
private final ByteArrayOutputStream err = new ByteArrayOutputStream();
private static final PrintStream OLD_OUT = System.out;
private static final PrintStream OLD_ERR = System.err;
private static final String DEFAULT_ENCODING = UTF_8.name();

@BeforeEach
public void setup() throws Exception {
System.setOut(new PrintStream(out, false, DEFAULT_ENCODING));
System.setErr(new PrintStream(err, false, DEFAULT_ENCODING));
}

@AfterEach
public void reset() {
// reset stream after each unit test
out.reset();
err.reset();

// restore system streams
System.setOut(OLD_OUT);
System.setErr(OLD_ERR);
}

@Test
void testOzoneRepairWhenUserIsOzoneDefaultUser() throws Exception {
OzoneRepair ozoneRepair = spy(new OzoneRepair());
doReturn(OZONE_DEFAULT_USER).when(ozoneRepair).getSystemUserName();
Comment thread
DaveTeng0 marked this conversation as resolved.
Outdated

ozoneRepair.execute(new String[]{});
assertThat(out.toString(DEFAULT_ENCODING)).contains("Run as user: " + OZONE_DEFAULT_USER);
}

@Test
void testOzoneRepairWhenUserIsNotOzoneDefaultUserAndDeclinesToProceed() throws Exception {
OzoneRepair ozoneRepair = spy(new OzoneRepair());
doReturn("non-ozone").when(ozoneRepair).getSystemUserName();
doReturn("N").when(ozoneRepair).getConsoleReadLineWithFormat(anyString(), anyString());

int res = ozoneRepair.execute(new String[]{});
assertEquals(1, res);
assertThat(out.toString(DEFAULT_ENCODING)).contains("Aborting command.");
}

@Test
void testOzoneRepairWhenUserIsNotOzoneDefaultUserAndAgreesToProceed() throws Exception {
OzoneRepair ozoneRepair = spy(new OzoneRepair());
String currentUser = "non-ozone";
doReturn(currentUser).when(ozoneRepair).getSystemUserName();
doReturn("y").when(ozoneRepair).getConsoleReadLineWithFormat(anyString(), anyString());

ozoneRepair.execute(new String[]{});
assertThat(out.toString(DEFAULT_ENCODING)).contains("Run as user: " + currentUser);
}

}