SOLR-8995: replace SAM implementations with lambda

This commit is contained in:
Noble Paul 2016-06-20 11:11:18 +05:30
parent 0179b3fb2d
commit bd9005d562
3 changed files with 36 additions and 52 deletions

View File

@ -180,35 +180,31 @@ class ShardLeaderElectionContextBase extends ElectionContext {
zcmd.ensureExists(parent, zkClient); zcmd.ensureExists(parent, zkClient);
try { try {
RetryUtil.retryOnThrowable(NodeExistsException.class, 60000, 5000, new RetryCmd() { RetryUtil.retryOnThrowable(NodeExistsException.class, 60000, 5000, () -> {
synchronized (lock) {
log.info("Creating leader registration node {} after winning as {}", leaderPath, leaderSeqPath);
List<Op> ops = new ArrayList<>(2);
@Override // We use a multi operation to get the parent nodes version, which will
public void execute() throws InterruptedException, KeeperException { // be used to make sure we only remove our own leader registration node.
synchronized (lock) { // The setData call used to get the parent version is also the trigger to
log.info("Creating leader registration node {} after winning as {}", leaderPath, leaderSeqPath); // increment the version. We also do a sanity check that our leaderSeqPath exists.
List<Op> ops = new ArrayList<>(2);
// We use a multi operation to get the parent nodes version, which will ops.add(Op.check(leaderSeqPath, -1));
// be used to make sure we only remove our own leader registration node. ops.add(Op.create(leaderPath, Utils.toJSON(leaderProps), zkClient.getZkACLProvider().getACLsToAdd(leaderPath), CreateMode.EPHEMERAL));
// The setData call used to get the parent version is also the trigger to ops.add(Op.setData(parent, null, -1));
// increment the version. We also do a sanity check that our leaderSeqPath exists. List<OpResult> results;
ops.add(Op.check(leaderSeqPath, -1)); results = zkClient.multi(ops, true);
ops.add(Op.create(leaderPath, Utils.toJSON(leaderProps), zkClient.getZkACLProvider().getACLsToAdd(leaderPath), CreateMode.EPHEMERAL)); for (OpResult result : results) {
ops.add(Op.setData(parent, null, -1)); if (result.getType() == ZooDefs.OpCode.setData) {
List<OpResult> results; SetDataResult dresult = (SetDataResult) result;
Stat stat = dresult.getStat();
results = zkClient.multi(ops, true); leaderZkNodeParentVersion = stat.getVersion();
for (OpResult result : results) { return;
if (result.getType() == ZooDefs.OpCode.setData) {
SetDataResult dresult = (SetDataResult) result;
Stat stat = dresult.getStat();
leaderZkNodeParentVersion = stat.getVersion();
return;
}
} }
assert leaderZkNodeParentVersion != null;
} }
assert leaderZkNodeParentVersion != null;
} }
}); });
} catch (Throwable t) { } catch (Throwable t) {

View File

@ -30,12 +30,12 @@ import org.slf4j.LoggerFactory;
public class RetryUtil { public class RetryUtil {
private static final Logger log = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass()); private static final Logger log = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
public static interface RetryCmd { public interface RetryCmd {
public void execute() throws Throwable; void execute() throws Throwable;
} }
public static interface BooleanRetryCmd { public interface BooleanRetryCmd {
public boolean execute(); boolean execute();
} }
public static void retryOnThrowable(Class clazz, long timeoutms, long intervalms, RetryCmd cmd) throws Throwable { public static void retryOnThrowable(Class clazz, long timeoutms, long intervalms, RetryCmd cmd) throws Throwable {

View File

@ -28,14 +28,10 @@ public class TestRetryUtil extends SolrTestCaseJ4 {
public void testRetryOnThrowable() throws Throwable { public void testRetryOnThrowable() throws Throwable {
final AtomicInteger executes = new AtomicInteger(); final AtomicInteger executes = new AtomicInteger();
RetryUtil.retryOnThrowable(SolrException.class, 10000, 10, new RetryCmd() { RetryUtil.retryOnThrowable(SolrException.class, 10000, 10, () -> {
int calls = executes.incrementAndGet();
@Override if (calls <= 2) {
public void execute() throws Throwable { throw new SolrException(ErrorCode.SERVER_ERROR, "Bad Stuff Happened");
int calls = executes.incrementAndGet();
if (calls <= 2) {
throw new SolrException(ErrorCode.SERVER_ERROR, "Bad Stuff Happened");
}
} }
}); });
@ -45,15 +41,11 @@ public class TestRetryUtil extends SolrTestCaseJ4 {
boolean caughtSolrException = false; boolean caughtSolrException = false;
try { try {
RetryUtil.retryOnThrowable(IllegalStateException.class, 10000, 10, RetryUtil.retryOnThrowable(IllegalStateException.class, 10000, 10,
new RetryCmd() { () -> {
int calls = executes2.incrementAndGet();
@Override if (calls <= 2) {
public void execute() throws Throwable { throw new SolrException(ErrorCode.SERVER_ERROR,
int calls = executes2.incrementAndGet(); "Bad Stuff Happened");
if (calls <= 2) {
throw new SolrException(ErrorCode.SERVER_ERROR,
"Bad Stuff Happened");
}
} }
}); });
} catch (SolrException e) { } catch (SolrException e) {
@ -65,13 +57,9 @@ public class TestRetryUtil extends SolrTestCaseJ4 {
final AtomicInteger executes3 = new AtomicInteger(); final AtomicInteger executes3 = new AtomicInteger();
caughtSolrException = false; caughtSolrException = false;
try { try {
RetryUtil.retryOnThrowable(SolrException.class, 1000, 10, new RetryCmd() { RetryUtil.retryOnThrowable(SolrException.class, 1000, 10, () -> {
executes3.incrementAndGet();
@Override throw new SolrException(ErrorCode.SERVER_ERROR, "Bad Stuff Happened");
public void execute() throws Throwable {
executes3.incrementAndGet();
throw new SolrException(ErrorCode.SERVER_ERROR, "Bad Stuff Happened");
}
}); });
} catch (SolrException e) { } catch (SolrException e) {
caughtSolrException = true; caughtSolrException = true;