This repository has been archived by the owner on Jun 7, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 292
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1063 from zalando/aruha-2306-revert-2
revert zookeeper lock PR
- Loading branch information
Showing
10 changed files
with
158 additions
and
147 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
60 changes: 60 additions & 0 deletions
60
src/main/java/org/zalando/nakadi/repository/zookeeper/ZkChildrenCache.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
package org.zalando.nakadi.repository.zookeeper; | ||
|
||
import com.google.common.collect.ImmutableList; | ||
import org.apache.curator.framework.CuratorFramework; | ||
import org.apache.curator.framework.recipes.cache.PathChildrenCache; | ||
import org.apache.zookeeper.KeeperException; | ||
import org.echocat.jomon.runtime.concurrent.RetryForSpecifiedCountStrategy; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import org.zalando.nakadi.exceptions.runtime.NakadiRuntimeException; | ||
|
||
import static org.apache.curator.framework.recipes.cache.PathChildrenCache.StartMode.BUILD_INITIAL_CACHE; | ||
import static org.echocat.jomon.runtime.concurrent.Retryer.executeWithRetry; | ||
|
||
public class ZkChildrenCache extends PathChildrenCache { | ||
|
||
private static final Logger LOG = LoggerFactory.getLogger(ZkChildrenCache.class); | ||
|
||
public static final int MAX_NUMBER_OF_RETRIES = 5; | ||
public static final int WAIT_BETWEEN_TRIES_MS = 100; | ||
|
||
public ZkChildrenCache(final CuratorFramework client, final String path) { | ||
super(client, path, false); | ||
} | ||
|
||
@Override | ||
public void start() throws Exception { | ||
try { | ||
super.start(BUILD_INITIAL_CACHE); | ||
} catch (final Exception e) { | ||
close(); | ||
throw e; | ||
} | ||
} | ||
|
||
public static ZkChildrenCache createCache(final CuratorFramework client, final String key) { | ||
try { | ||
// in some rare case the cache start can fail because the node can be removed | ||
// in specific moment by other thread/instance, then we need to retry | ||
return executeWithRetry( | ||
() -> { | ||
final ZkChildrenCache newCache = new ZkChildrenCache(client, key); | ||
try { | ||
newCache.start(); | ||
return newCache; | ||
} catch (final KeeperException.NoNodeException e) { | ||
throw e; // throw it to activate retry | ||
} catch (final Exception e) { | ||
throw new NakadiRuntimeException(e); | ||
} | ||
}, | ||
new RetryForSpecifiedCountStrategy<ZkChildrenCache>(MAX_NUMBER_OF_RETRIES) | ||
.withExceptionsThatForceRetry(ImmutableList.of(KeeperException.NoNodeException.class)) | ||
.withWaitBetweenEachTry(WAIT_BETWEEN_TRIES_MS)); | ||
} catch (final Exception e) { | ||
LOG.error("Zookeeper error when creating cache for children", e); | ||
throw new NakadiRuntimeException(e); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
17 changes: 17 additions & 0 deletions
17
src/main/java/org/zalando/nakadi/repository/zookeeper/ZooKeeperLockFactory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package org.zalando.nakadi.repository.zookeeper; | ||
|
||
import org.apache.curator.framework.recipes.locks.InterProcessLock; | ||
import org.apache.curator.framework.recipes.locks.InterProcessSemaphoreMutex; | ||
|
||
public class ZooKeeperLockFactory { | ||
|
||
private final ZooKeeperHolder zkHolder; | ||
|
||
public ZooKeeperLockFactory(final ZooKeeperHolder zkHolder) { | ||
this.zkHolder = zkHolder; | ||
} | ||
|
||
public InterProcessLock createLock(final String path) { | ||
return new InterProcessSemaphoreMutex(zkHolder.get(), path); | ||
} | ||
} |
106 changes: 0 additions & 106 deletions
106
src/main/java/org/zalando/nakadi/repository/zookeeper/ZookeeperLock.java
This file was deleted.
Oops, something went wrong.
28 changes: 28 additions & 0 deletions
28
src/main/java/org/zalando/nakadi/repository/zookeeper/ZookeeperUtils.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package org.zalando.nakadi.repository.zookeeper; | ||
|
||
import org.apache.curator.framework.recipes.locks.InterProcessLock; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import java.util.concurrent.Callable; | ||
|
||
public class ZookeeperUtils { | ||
|
||
private static final Logger LOG = LoggerFactory.getLogger(ZookeeperUtils.class); | ||
|
||
private ZookeeperUtils() { | ||
} | ||
|
||
public static <V> V runLocked(final Callable<V> callable, final InterProcessLock lock) throws Exception { | ||
lock.acquire(); | ||
try { | ||
return callable.call(); | ||
} finally { | ||
try { | ||
lock.release(); | ||
} catch (final Exception e) { | ||
LOG.warn("Error occurred when releasing ZK lock", e); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.