Skip to content

Instantly share code, notes, and snippets.

package com.netflix.curator.framework.recipes.locks;
import com.google.common.util.concurrent.ThreadFactoryBuilder;
import com.netflix.curator.framework.CuratorFramework;
import org.apache.zookeeper.KeeperException;
import org.apache.zookeeper.data.Stat;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.Closeable;
import java.io.IOException;
public class InterProcessSemaphoreMutex implements InterProcessLock
{
private InterProcessSemaphore semaphore;
private volatile Lease lease;
public InterProcessSemaphoreMutex(InterProcessSemaphore semaphore)
{
this.semaphore = semaphore;
}
public void testKilledServerWithEnsembleProvider() throws Exception
{
final int CLIENT_QTY = 10;
final Timing timing = new Timing();
final String PATH = "/foo/bar/lock";
ExecutorService executorService = Executors.newFixedThreadPool(CLIENT_QTY);
ExecutorCompletionService<Void> completionService = new ExecutorCompletionService<Void>(executorService);
TestingCluster cluster = new TestingCluster(3);
try
@Randgalt
Randgalt / FailoverTests.java
Created September 11, 2012 20:30
FailoverTests
import com.google.common.io.Closeables;
import com.netflix.curator.framework.CuratorFramework;
import com.netflix.curator.framework.CuratorFrameworkFactory;
import com.netflix.curator.framework.api.BackgroundCallback;
import com.netflix.curator.framework.api.CuratorEvent;
import com.netflix.curator.retry.ExponentialBackoffRetry;
import com.netflix.curator.test.InstanceSpec;
import com.netflix.curator.test.TestingCluster;
import org.apache.zookeeper.AsyncCallback;
import org.apache.zookeeper.WatchedEvent;
@Randgalt
Randgalt / FourLetterWord.java
Created September 11, 2012 20:32
FourLetterWord
import com.google.common.base.Preconditions;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.io.CharStreams;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.StringReader;
import java.net.Socket;
import java.util.List;
@Randgalt
Randgalt / gist:3784312
Created September 25, 2012 20:43
InterProcessSemaphoreV2.java
/*
* Copyright 2012 Netflix, Inc.
*
* Licensed 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
package com.netflix.curator.framework.recipes.nodes;
import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.Objects;
import com.google.common.base.Preconditions;
import com.netflix.curator.framework.CuratorFramework;
import com.netflix.curator.framework.api.ACLBackgroundPathAndBytesable;
import com.netflix.curator.framework.api.BackgroundCallback;
import com.netflix.curator.framework.api.CreateModable;
import com.netflix.curator.framework.api.CuratorEvent;
private void offerOperation(final Operation operation)
{
if ( operationsQuantizer.add(operation) )
{
submitToExecutor
(
new Runnable()
{
@Override
public void run()
@Randgalt
Randgalt / gist:5988052
Last active December 19, 2015 16:59
Alternative implementation of DispatchingWatcher
package org.apache.curator.framework.imps;
import com.google.common.cache.CacheBuilder;
import com.google.common.cache.CacheLoader;
import com.google.common.cache.LoadingCache;
import com.google.common.collect.Maps;
import com.google.common.collect.Sets;
import org.apache.zookeeper.ClientCnxn;
import org.apache.zookeeper.WatchedEvent;
import org.apache.zookeeper.Watcher;
final AtomicBoolean hasLeadership = new AtomicBoolean(false);
public LeaderSelector startParticipant(CuratorFramework client, int id) throws Exception {
LeaderSelector selector = null;
final int myid = id;
LeaderSelectorListener listener = new LeaderSelectorListener()
{
volatile CountDownLatch holdLatch;
@Override