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
Expand Up @@ -88,8 +88,7 @@ public void reinitialize(SCMMetadataStore arg0) {
@Override
public List<X509Certificate> removeAllExpiredCertificates() throws IOException {
final Object[] args = {};
return (List<X509Certificate>) invoker.invokeReplicateDirect(ReplicateMethod.removeAllExpiredCertificates,
args);
return (List)invoker.invokeReplicateDirect(ReplicateMethod.removeAllExpiredCertificates, args);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,12 +116,12 @@ public List<ContainerID> getContainerIDs(LifeCycleState arg0, ContainerID arg1,
}

@Override
public List<ContainerInfo> getContainerInfos(ReplicationType arg0) {
public List<ContainerInfo> getContainerInfos(LifeCycleState arg0) {
return invoker.getImpl().getContainerInfos(arg0);
}

@Override
public List<ContainerInfo> getContainerInfos(LifeCycleState arg0) {
public List<ContainerInfo> getContainerInfos(ReplicationType arg0) {
return invoker.getImpl().getContainerInfos(arg0);
}

Expand Down Expand Up @@ -231,14 +231,14 @@ public Message invokeLocal(String methodName, Object[] p) throws Exception {
break;

case "getContainerInfos":
if (p.length == 1 && (p[0] == null || ReplicationType.class.isInstance(p[0]))) {
final ReplicationType arg7 = (ReplicationType) p[0];
if (p.length == 1 && (p[0] == null || LifeCycleState.class.isInstance(p[0]))) {
final LifeCycleState arg7 = (LifeCycleState) p[0];
returnType = List.class;
returnValue = getImpl().getContainerInfos(arg7);
break;
}
if (p.length == 1 && (p[0] == null || LifeCycleState.class.isInstance(p[0]))) {
final LifeCycleState arg8 = (LifeCycleState) p[0];
if (p.length == 1 && (p[0] == null || ReplicationType.class.isInstance(p[0]))) {
final ReplicationType arg8 = (ReplicationType) p[0];
returnType = List.class;
returnValue = getImpl().getContainerInfos(arg8);
break;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,9 @@
import java.util.stream.Stream;
import org.apache.commons.lang3.StringUtils;
import org.apache.hadoop.hdds.scm.metadata.Replicate;
import org.apache.ratis.io.MD5Hash;
import org.apache.ratis.protocol.Message;
import org.apache.ratis.util.MD5FileUtil;
import org.apache.ratis.util.Preconditions;
import org.apache.ratis.util.UncheckedAutoCloseable;

Expand Down Expand Up @@ -82,7 +84,7 @@ public final class ScmInvokerCodeGenerator {
private final StringWriter out = new StringWriter();
private String indentation = "";

private ScmInvokerCodeGenerator(Class<?> api) {
ScmInvokerCodeGenerator(Class<?> api) {
this.api = api;
this.apiName = api.getSimpleName();
this.invokerClassName = getInvokerClassName(api);
Expand Down Expand Up @@ -296,7 +298,9 @@ List<Method> getMethods(Boolean isDefault, Boolean isDeprecated) {
List<Method> getMethods(Predicate<Method> filter) {
return Arrays.stream(api.getMethods())
.filter(filter)
.sorted(Comparator.comparing(Method::getName).thenComparing(Method::getParameterCount))
.sorted(Comparator.comparing(Method::getName)
.thenComparing(Method::getParameterCount)
.thenComparing(m -> Arrays.toString(m.getParameterTypes())))
.collect(Collectors.toList());
}

Expand Down Expand Up @@ -607,15 +611,17 @@ public String generateClass() {
return out.toString();
}

File updateFile(String classString) throws IOException {
final File java = new File(DIR, invokerClassName + ".java");
File updateFile(String classString, String dir, boolean overwrite) throws IOException {
final File java = new File(dir, invokerClassName + ".java");
if (!java.isFile()) {
throw new FileNotFoundException("Not found: " + java.getAbsolutePath());
}
final File tmp = new File(DIR, invokerClassName + "_tmp.java");
final File tmp = new File(dir, invokerClassName + "_tmp.java");
if (tmp.exists()) {
throw new IOException("Already exist: " + java.getAbsolutePath());
}
tmp.deleteOnExit();

try (InputStream inStream = Files.newInputStream(java.toPath());
BufferedReader in = new BufferedReader(new InputStreamReader(new BufferedInputStream(inStream), UTF_8));
OutputStream outStream = Files.newOutputStream(tmp.toPath(), StandardOpenOption.CREATE_NEW);
Expand All @@ -634,8 +640,18 @@ File updateFile(String classString) throws IOException {
out.print(classString);
}

Files.move(tmp.toPath(), java.toPath(), StandardCopyOption.REPLACE_EXISTING);
return java;
final MD5Hash javaMd5 = MD5FileUtil.computeMd5ForFile(java);
final MD5Hash tmpMd5 = MD5FileUtil.computeMd5ForFile(tmp);
if (Arrays.equals(javaMd5.getDigest(), tmpMd5.getDigest())) {
Files.delete(tmp.toPath());
return null;
}
if (overwrite) {
Files.move(tmp.toPath(), java.toPath(), StandardCopyOption.REPLACE_EXISTING);
return java;
} else {
return tmp;
}
}

public static void generate(Class<?> api, boolean updateFile) {
Expand All @@ -648,11 +664,15 @@ public static void generate(Class<?> api, boolean updateFile) {

final File file;
try {
file = generator.updateFile(classString);
file = generator.updateFile(classString, DIR, true);
} catch (IOException e) {
throw new IllegalStateException("Failed to updateFile", e);
}
System.out.printf("Successfully update file: %s%n", file);
if (file == null) {
System.out.printf("No change for %s%n", getInvokerClassName(api));
} else {
System.out.printf("Successfully update file: %s%n", file);
}
}

static class DeclaredMethod {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,56 +31,70 @@
class ScmInvokerCodeGeneratorMains {

static class GenerateDeletedBlockLogStateManager {
public static void main(String[] args) {
public static void main(String... args) {
ScmInvokerCodeGenerator.generate(DeletedBlockLogStateManager.class, true);
}
}

static class GenerateContainerStateManager {
public static void main(String[] args) {
public static void main(String... args) {
ScmInvokerCodeGenerator.generate(ContainerStateManager.class, true);
}
}

static class GeneratePipelineStateManager {
public static void main(String[] args) {
public static void main(String... args) {
ScmInvokerCodeGenerator.generate(PipelineStateManager.class, true);
}
}

static class GenerateRootCARotationHandler {
public static void main(String[] args) {
public static void main(String... args) {
ScmInvokerCodeGenerator.generate(RootCARotationHandler.class, true);
}
}

static class GenerateFinalizationStateManager {
public static void main(String[] args) {
public static void main(String... args) {
ScmInvokerCodeGenerator.generate(FinalizationStateManager.class, true);
}
}

static class GenerateSecretKeyState {
public static void main(String[] args) {
public static void main(String... args) {
ScmInvokerCodeGenerator.generate(SecretKeyState.class, true);
}
}

static class GenerateSequenceIdGeneratorStateManager {
public static void main(String[] args) {
public static void main(String... args) {
ScmInvokerCodeGenerator.generate(SequenceIdGenerator.StateManager.class, true);
}
}

static class GenerateStatefulServiceStateManager {
public static void main(String[] args) {
public static void main(String... args) {
ScmInvokerCodeGenerator.generate(StatefulServiceStateManager.class, true);
}
}

static class GenerateCertificateStore {
public static void main(String[] args) {
public static void main(String... args) {
ScmInvokerCodeGenerator.generate(CertificateStore.class, true);
}
}

static class All {
public static void main(String... args) {
GenerateCertificateStore.main();
GenerateContainerStateManager.main();
GenerateDeletedBlockLogStateManager.main();
GenerateFinalizationStateManager.main();
GeneratePipelineStateManager.main();
GenerateRootCARotationHandler.main();
GenerateSecretKeyState.main();
GenerateSequenceIdGeneratorStateManager.main();
GenerateStatefulServiceStateManager.main();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
/*
* 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.hdds.scm.ha.invoker;

import static org.junit.jupiter.api.Assertions.assertNull;

import java.io.File;
import org.apache.hadoop.hdds.scm.block.DeletedBlockLogStateManager;
import org.apache.hadoop.hdds.scm.container.ContainerStateManager;
import org.apache.hadoop.hdds.scm.ha.SequenceIdGenerator;
import org.apache.hadoop.hdds.scm.ha.StatefulServiceStateManager;
import org.apache.hadoop.hdds.scm.pipeline.PipelineStateManager;
import org.apache.hadoop.hdds.scm.security.RootCARotationHandler;
import org.apache.hadoop.hdds.scm.server.upgrade.FinalizationStateManager;
import org.apache.hadoop.hdds.security.symmetric.SecretKeyState;
import org.apache.hadoop.hdds.security.x509.certificate.authority.CertificateStore;
import org.junit.jupiter.api.Test;

/** Test the code generated by {@link ScmInvokerCodeGenerator}. */
public final class TestScmInvokerCodeGenerator {
static final String DIR = "src/main/java/org/apache/hadoop/hdds/scm/ha/invoker/";

static void runTest(Class<?> api) throws Exception {
final ScmInvokerCodeGenerator generator = new ScmInvokerCodeGenerator(api);
final String classString = generator.generateClass();
final File file = generator.updateFile(classString, DIR, false);
assertNull(file, () -> ScmInvokerCodeGenerator.getInvokerClassName(api) + " is changed.");
}

@Test
public void testDeletedBlockLogStateManager() throws Exception {
runTest(DeletedBlockLogStateManager.class);
}

@Test
public void testContainerStateManager() throws Exception {
runTest(ContainerStateManager.class);
}

@Test
public void testPipelineStateManager() throws Exception {
runTest(PipelineStateManager.class);
}

@Test
public void testRootCARotationHandler() throws Exception {
runTest(RootCARotationHandler.class);
}

@Test
public void testFinalizationStateManager() throws Exception {
runTest(FinalizationStateManager.class);
}

@Test
public void testSecretKeyState() throws Exception {
runTest(SecretKeyState.class);
}

@Test
public void testSequenceIdGeneratorStateManager() throws Exception {
runTest(SequenceIdGenerator.StateManager.class);
}

@Test
public void testStatefulServiceStateManager() throws Exception {
runTest(StatefulServiceStateManager.class);
}

@Test
public void testCertificateStore() throws Exception {
runTest(CertificateStore.class);
}
}