Skip to content

Commit

Permalink
RATIS-52 Refactor RaftUtils into different classes
Browse files Browse the repository at this point in the history
  • Loading branch information
enis committed Apr 10, 2017
1 parent 9bd8cbd commit 43bc6eb
Show file tree
Hide file tree
Showing 80 changed files with 926 additions and 731 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
import org.apache.ratis.shaded.proto.RaftProtos.*;
import org.apache.ratis.protocol.*;
import org.apache.ratis.util.ProtoUtils;
import org.apache.ratis.util.RaftUtils;
import org.apache.ratis.util.ReflectionUtils;

import java.util.Arrays;

Expand Down Expand Up @@ -143,7 +143,7 @@ private static StateMachineException wrapStateMachineException(
} else {
try {
Class<?> clazz = Class.forName(className);
final Exception e = RaftUtils.instantiateException(
final Exception e = ReflectionUtils.instantiateException(
clazz.asSubclass(Exception.class), errorMsg, null);
sme = new StateMachineException(serverId, e);
} catch (Exception e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,10 @@

import org.apache.ratis.client.RaftClient;
import org.apache.ratis.client.RaftClientRpc;
import org.apache.ratis.util.IOUtils;
import org.apache.ratis.util.CollectionUtils;
import org.apache.ratis.util.TimeDuration;
import org.apache.ratis.protocol.*;
import org.apache.ratis.util.RaftUtils;

import java.io.IOException;
import java.io.InterruptedIOException;
Expand Down Expand Up @@ -108,7 +109,7 @@ private RaftClientReply sendRequestWithRetry(
retryInterval.sleep();
} catch (InterruptedException ie) {
Thread.currentThread().interrupt();
throw RaftUtils.toInterruptedIOException(
throw IOUtils.toInterruptedIOException(
"Interrupted when sending " + request, ie);
}
}
Expand Down Expand Up @@ -158,7 +159,7 @@ private void handleIOException(RaftClientRequest request, IOException ioe,
newLeader, ioe);
final RaftPeerId oldLeader = request.getServerId();
if (newLeader == null && oldLeader.equals(leaderId)) {
newLeader = RaftUtils.next(oldLeader, RaftUtils.as(peers, RaftPeer::getId));
newLeader = CollectionUtils.next(oldLeader, CollectionUtils.as(peers, RaftPeer::getId));
}
if (newLeader != null && oldLeader.equals(leaderId)) {
LOG.debug("{}: change Leader from {} to {}", clientId, oldLeader, newLeader);
Expand Down
12 changes: 7 additions & 5 deletions ratis-common/src/main/java/org/apache/ratis/RaftConfigKeys.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,14 @@
*/
package org.apache.ratis;

import static org.apache.ratis.conf.ConfUtils.get;
import static org.apache.ratis.conf.ConfUtils.printAll;
import static org.apache.ratis.conf.ConfUtils.set;

import org.apache.ratis.conf.RaftProperties;
import org.apache.ratis.rpc.RpcType;
import org.apache.ratis.rpc.SupportedRpcType;
import org.apache.ratis.util.RaftUtils;

import static org.apache.ratis.conf.ConfUtils.*;
import org.apache.ratis.util.ReflectionUtils;

public interface RaftConfigKeys {
String PREFIX = "raft";
Expand All @@ -42,8 +44,8 @@ static RpcType type(RaftProperties properties) {
}

// Try using it as a class name
return RaftUtils.newInstance(
RaftUtils.getClass(t, properties, RpcType.class));
return ReflectionUtils.newInstance(
ReflectionUtils.getClass(t, properties, RpcType.class));
}

static void setType(RaftProperties properties, RpcType type) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,9 @@
package org.apache.ratis.io.nativeio;

import org.apache.ratis.protocol.AlreadyExistsException;
import org.apache.ratis.util.IOUtils;
import org.apache.ratis.util.NativeCodeLoader;
import org.apache.ratis.util.RaftUtils;
import org.apache.ratis.util.PlatformUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import sun.misc.Unsafe;
Expand Down Expand Up @@ -183,7 +184,7 @@ private static void assertCodeLoaded() throws IOException {
private static native void chmodImpl(String path, int mode) throws IOException;

public static void chmod(String path, int mode) throws IOException {
if (!RaftUtils.WINDOWS) {
if (!PlatformUtils.WINDOWS) {
chmodImpl(path, mode);
} else {
try {
Expand Down Expand Up @@ -321,12 +322,12 @@ public static class Stat {
}

Stat(String owner, String group, int mode) {
if (!RaftUtils.WINDOWS) {
if (!PlatformUtils.WINDOWS) {
this.owner = owner;
} else {
this.owner = stripDomain(owner);
}
if (!RaftUtils.WINDOWS) {
if (!PlatformUtils.WINDOWS) {
this.group = group;
} else {
this.group = stripDomain(group);
Expand Down Expand Up @@ -604,7 +605,7 @@ private static String stripDomain(String name) {
*/
public static FileInputStream getShareDeleteFileInputStream(File f)
throws IOException {
if (!RaftUtils.WINDOWS) {
if (!PlatformUtils.WINDOWS) {
// On Linux the default FileInputStream shares delete permission
// on the file opened.
//
Expand Down Expand Up @@ -632,7 +633,7 @@ public static FileInputStream getShareDeleteFileInputStream(File f)
*/
public static FileInputStream getShareDeleteFileInputStream(File f, long seekOffset)
throws IOException {
if (!RaftUtils.WINDOWS) {
if (!PlatformUtils.WINDOWS) {
RandomAccessFile rf = new RandomAccessFile(f, "r");
if (seekOffset > 0) {
rf.seek(seekOffset);
Expand Down Expand Up @@ -666,7 +667,7 @@ public static FileInputStream getShareDeleteFileInputStream(File f, long seekOff
*/
public static FileOutputStream getCreateForWriteFileOutputStream(File f, int permissions)
throws IOException {
if (!RaftUtils.WINDOWS) {
if (!PlatformUtils.WINDOWS) {
// Use the native wrapper around open(2)
try {
FileDescriptor fd = NativeIO.POSIX.open(f.getAbsolutePath(),
Expand Down Expand Up @@ -770,7 +771,7 @@ private static native void link0(String src, String dst)
* @param dst The destination path
*/
public static void copyFileUnbuffered(File src, File dst) throws IOException {
if (nativeLoaded && RaftUtils.WINDOWS) {
if (nativeLoaded && PlatformUtils.WINDOWS) {
copyFileUnbuffered0(src.getAbsolutePath(), dst.getAbsolutePath());
} else {
FileInputStream fis = null;
Expand All @@ -791,7 +792,7 @@ public static void copyFileUnbuffered(File src, File dst) throws IOException {
position += transferred;
}
} finally {
RaftUtils.cleanup(LOG, output, fos, input, fis);
IOUtils.cleanup(LOG, output, fos, input, fis);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

import java.io.IOException;

import org.apache.ratis.util.RaftUtils;
import org.apache.ratis.util.PlatformUtils;


/**
Expand Down Expand Up @@ -61,7 +61,7 @@ public Errno getErrno() {

@Override
public String toString() {
if (RaftUtils.WINDOWS)
if (PlatformUtils.WINDOWS)
return errorCode + ": " + super.getMessage();
else
return errno.toString() + ": " + super.getMessage();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
*/
package org.apache.ratis.protocol;

import org.apache.ratis.util.RaftUtils;
import org.apache.ratis.util.Preconditions;

import java.nio.ByteBuffer;
import java.util.Objects;
Expand All @@ -43,7 +43,7 @@ private ClientId(UUID id) {

public ClientId(byte[] data) {
Objects.requireNonNull(data, "data == null");
RaftUtils.assertTrue(data.length == BYTE_LENGTH,
Preconditions.assertTrue(data.length == BYTE_LENGTH,
"data.length = %s != BYTE_LENGTH = %s", data.length, BYTE_LENGTH);
ByteBuffer buffer = ByteBuffer.wrap(data);
this.uuid = new UUID(buffer.getLong(), buffer.getLong());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
package org.apache.ratis.protocol;

import org.apache.ratis.shaded.com.google.protobuf.ByteString;
import org.apache.ratis.util.RaftUtils;
import org.apache.ratis.util.Preconditions;

import java.nio.charset.StandardCharsets;
import java.util.Arrays;
Expand All @@ -37,7 +37,7 @@ public static RaftPeerId getRaftPeerId(String id) {

public RaftPeerId(String id) {
Objects.requireNonNull(id, "id == null");
RaftUtils.assertTrue(!id.isEmpty(), "id is an empty string.");
Preconditions.assertTrue(!id.isEmpty(), "id is an empty string.");
this.id = id.getBytes(StandardCharsets.UTF_8);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

import org.apache.ratis.conf.Parameters;
import org.apache.ratis.conf.RaftProperties;
import org.apache.ratis.util.RaftUtils;
import org.apache.ratis.util.ReflectionUtils;

/** The RPC types supported. */
public enum SupportedRpcType implements RpcType {
Expand All @@ -42,8 +42,8 @@ public static SupportedRpcType valueOfIgnoreCase(String s) {

@Override
public RpcFactory newFactory(RaftProperties properties, Parameters parameters) {
final Class<? extends RpcFactory> clazz = RaftUtils.getClass(
final Class<? extends RpcFactory> clazz = ReflectionUtils.getClass(
factoryClassName, properties, RpcFactory.class);
return RaftUtils.newInstance(clazz, ARG_CLASSES, parameters);
return ReflectionUtils.newInstance(clazz, ARG_CLASSES, parameters);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ public void close() throws IOException {
} else {
if (!triedToClose) {
// If we failed when flushing, try to close it to not leak an FD
RaftUtils.cleanup(LOG, out);
IOUtils.cleanup(LOG, out);
}
// close wasn't successful, try to delete the tmp file
if (!tmpFile.delete()) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/*
* *
* * 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.ratis.util;

import java.util.Iterator;
import java.util.Objects;
import java.util.function.Function;

public interface CollectionUtils {
/**
* @return the next element in the iteration right after the given element;
* if the given element is not in the iteration, return the first one
*/
static <T> T next(final T given, final Iterable<T> iteration) {
Objects.requireNonNull(given, "given == null");
final Iterator<T> i = Objects.requireNonNull(iteration, "iteration == null").iterator();
Preconditions.assertTrue(i.hasNext(), "iteration is empty.");

final T first = i.next();
for(T current = first; i.hasNext(); ) {
final T next = i.next();
if (given.equals(current)) {
return next;
}
current = next;
}
return first;
}

static <INPUT, OUTPUT> Iterable<OUTPUT> as(
Iterable<INPUT> iteration, Function<INPUT, OUTPUT> converter) {
return () -> new Iterator<OUTPUT>() {
final Iterator<INPUT> i = iteration.iterator();
@Override
public boolean hasNext() {
return i.hasNext();
}

@Override
public OUTPUT next() {
return converter.apply(i.next());
}
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ public static File[] listFiles(File dir) throws IOException {
* On Windows, true if process has write access on the path
*/
public static boolean canWrite(File f) {
if (RaftUtils.WINDOWS) {
if (PlatformUtils.WINDOWS) {
try {
return NativeIO.Windows.access(f.getCanonicalPath(),
NativeIO.Windows.AccessRight.ACCESS_WRITE);
Expand Down
Loading

0 comments on commit 43bc6eb

Please sign in to comment.