Examples of HelixLock


Examples of org.apache.helix.lock.HelixLock

  }

  @Override
  public Resource readResource(ResourceId resourceId) {
    ClusterId clusterId = clusterId();
    HelixLock lock = _lockProvider.getLock(clusterId, Scope.resource(resourceId));
    boolean locked = lock.lock();
    if (locked) {
      try {
        return _clusterAccessor.readResource(resourceId);
      } finally {
        lock.unlock();
      }
    }
    return null;
  }
View Full Code Here

Examples of org.apache.helix.lock.HelixLock

  }

  @Override
  public ResourceConfig updateResource(ResourceId resourceId, ResourceConfig.Delta resourceDelta) {
    ClusterId clusterId = clusterId();
    HelixLock lock = _lockProvider.getLock(clusterId, Scope.resource(resourceId));
    boolean locked = lock.lock();
    if (locked) {
      try {
        return _clusterAccessor.updateResource(resourceId, resourceDelta);
      } finally {
        lock.unlock();
      }
    }
    return null;
  }
View Full Code Here

Examples of org.apache.helix.lock.HelixLock

    final AtomicBoolean t1Locked = new AtomicBoolean(false);
    final AtomicBoolean t1Done = new AtomicBoolean(false);
    final AtomicInteger field1 = new AtomicInteger(0);
    final AtomicInteger field2 = new AtomicInteger(1);
    final ClusterId clusterId = ClusterId.from("testCluster");
    final HelixLock lock1 = new ZKHelixLock(clusterId, Scope.cluster(clusterId), _zkclient);
    final HelixLock lock2 = new ZKHelixLock(clusterId, Scope.cluster(clusterId), _zkclient);

    // thread 1: get a lock, set fields to 1
    Thread t1 = new Thread() {
      @Override
      public void run() {
        lock1.lock();
        synchronized (t1Locked) {
          t1Locked.set(true);
          t1Locked.notify();
        }
        yield(); // if locking doesn't work, t2 will set the fields first
        field1.set(1);
        field2.set(1);
        synchronized (t1Done) {
          t1Done.set(true);
          t1Done.notify();
        }
      }
    };

    // thread 2: wait for t1 to acquire the lock, get a lock, set fields to 2
    Thread t2 = new Thread() {
      @Override
      public void run() {
        synchronized (t1Locked) {
          while (!t1Locked.get()) {
            try {
              t1Locked.wait();
            } catch (InterruptedException e) {
            }
          }
        }
        lock2.lock();
        field1.set(2);
        field2.set(2);
      }
    };

    // start the threads
    t1.setPriority(Thread.MIN_PRIORITY);
    t2.setPriority(Thread.MAX_PRIORITY);
    t1.start();
    t2.start();

    // wait for t1 to finish setting fields
    synchronized (t1Done) {
      while (!t1Done.get()) {
        try {
          t1Done.wait();
        } catch (InterruptedException e) {
        }
      }
    }

    // make sure both fields are 1
    Assert.assertEquals(field1.get(), 1);
    Assert.assertEquals(field2.get(), 1);

    // unlock t1's lock after checking that t2 is blocked
    long count = 0;
    while (!lock2.isBlocked()) {
      if (count > TIMEOUT) {
        break;
      }
      Thread.sleep(RETRY_INTERVAL);
      count += RETRY_INTERVAL;
    }
    Assert.assertTrue(lock2.isBlocked());
    lock1.unlock();

    try {
      // wait for t2, make sure both fields are 2
      t2.join(10000);
View Full Code Here

Examples of org.apache.helix.lock.HelixLock

    final AtomicBoolean t1Locked = new AtomicBoolean(false);
    final AtomicBoolean t1Done = new AtomicBoolean(false);
    final AtomicInteger field1 = new AtomicInteger(0);
    final AtomicInteger field2 = new AtomicInteger(1);
    final ClusterId clusterId = ClusterId.from("testCluster");
    final HelixLock lock1 = new ZKHelixLock(clusterId, Scope.cluster(clusterId), _gZkClient);
    final HelixLock lock2 = new ZKHelixLock(clusterId, Scope.cluster(clusterId), _gZkClient);

    // thread 1: get a lock, set fields to 1
    Thread t1 = new Thread() {
      @Override
      public void run() {
        lock1.lock();
        synchronized (t1Locked) {
          t1Locked.set(true);
          t1Locked.notify();
        }
        yield(); // if locking doesn't work, t2 will set the fields first
        field1.set(1);
        field2.set(1);
        synchronized (t1Done) {
          t1Done.set(true);
          t1Done.notify();
        }
      }
    };

    // thread 2: wait for t1 to acquire the lock, get a lock, set fields to 2
    Thread t2 = new Thread() {
      @Override
      public void run() {
        synchronized (t1Locked) {
          while (!t1Locked.get()) {
            try {
              t1Locked.wait();
            } catch (InterruptedException e) {
            }
          }
        }
        lock2.lock();
        field1.set(2);
        field2.set(2);
      }
    };

    // start the threads
    t1.setPriority(Thread.MIN_PRIORITY);
    t2.setPriority(Thread.MAX_PRIORITY);
    t1.start();
    t2.start();

    // wait for t1 to finish setting fields
    synchronized (t1Done) {
      while (!t1Done.get()) {
        try {
          t1Done.wait();
        } catch (InterruptedException e) {
        }
      }
    }

    // make sure both fields are 1
    Assert.assertEquals(field1.get(), 1);
    Assert.assertEquals(field2.get(), 1);

    // unlock t1's lock after checking that t2 is blocked
    long count = 0;
    while (!lock2.isBlocked()) {
      if (count > TIMEOUT) {
        break;
      }
      Thread.sleep(RETRY_INTERVAL);
      count += RETRY_INTERVAL;
    }
    Assert.assertTrue(lock2.isBlocked());
    lock1.unlock();

    try {
      // wait for t2, make sure both fields are 2
      t2.join(10000);
View Full Code Here

Examples of org.apache.helix.lock.HelixLock

    _resourceAccessor = new ResourceAccessor(accessor);
  }

  @Override
  public Resource readResource(ResourceId resourceId) {
    HelixLock lock = _lockProvider.getLock(_clusterId, Scope.resource(resourceId));
    boolean locked = lock.lock();
    if (locked) {
      try {
        return _resourceAccessor.readResource(resourceId);
      } finally {
        lock.unlock();
      }
    }
    return null;
  }
View Full Code Here

Examples of org.apache.helix.lock.HelixLock

    return null;
  }

  @Override
  public ResourceConfig updateResource(ResourceId resourceId, ResourceConfig.Delta resourceDelta) {
    HelixLock lock = _lockProvider.getLock(_clusterId, Scope.resource(resourceId));
    boolean locked = lock.lock();
    if (locked) {
      try {
        return _resourceAccessor.updateResource(resourceId, resourceDelta);
      } finally {
        lock.unlock();
      }
    }
    return null;
  }
View Full Code Here

Examples of org.apache.helix.lock.HelixLock

    return null;
  }

  @Override
  public boolean setRebalancerContext(ResourceId resourceId, RebalancerContext context) {
    HelixLock lock = _lockProvider.getLock(_clusterId, Scope.resource(resourceId));
    boolean locked = lock.lock();
    if (locked) {
      try {
        return _resourceAccessor.setRebalancerContext(resourceId, context);
      } finally {
        lock.unlock();
      }
    }
    return false;
  }
View Full Code Here

Examples of org.apache.helix.lock.HelixLock

  public boolean setResource(ResourceConfig resourceConfig) {
    if (resourceConfig == null) {
      LOG.error("resource config cannot be null");
      return false;
    }
    HelixLock lock = _lockProvider.getLock(_clusterId, Scope.resource(resourceConfig.getId()));
    boolean locked = lock.lock();
    if (locked) {
      try {
        return _resourceAccessor.setResource(resourceConfig);
      } finally {
        lock.unlock();
      }
    }
    return false;
  }
View Full Code Here

Examples of org.apache.helix.lock.HelixLock

  }

  @Override
  public boolean generateDefaultAssignment(ResourceId resourceId, int replicaCount,
      String participantGroupTag) {
    HelixLock lock = _lockProvider.getLock(_clusterId, Scope.cluster(_clusterId));
    boolean locked = lock.lock();
    if (locked) {
      try {
        return _resourceAccessor.generateDefaultAssignment(resourceId, replicaCount,
            participantGroupTag);
      } finally {
        lock.unlock();
      }
    }
    return false;
  }
View Full Code Here

Examples of org.apache.helix.lock.HelixLock

    _clusterAccessor = new ClusterAccessor(clusterId, accessor);
  }

  @Override
  public boolean createCluster(ClusterConfig cluster) {
    HelixLock lock = _lockProvider.getLock(_clusterId, Scope.cluster(_clusterId));
    boolean locked = lock.lock();
    if (locked) {
      try {
        return _clusterAccessor.createCluster(cluster);
      } finally {
        lock.unlock();
      }
    }
    return false;
  }
View Full Code Here
TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.