All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.linkedin.parseq.zk.recipes.MultiLocks Maven / Gradle / Ivy

There is a newer version: 4.0.1
Show newest version
package com.linkedin.parseq.zk.recipes;

import com.linkedin.parseq.Task;
import com.linkedin.parseq.zk.client.ZKClient;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;


/**
 * This class represents multiple zookeeper locks that has to be
 * acquired in an atomic manner. Note that Locks are acquired in
 * the natural ordering of its lockPath to avoid deadlock.
 *
 * @author Ang Xu
 */
public class MultiLocks implements Synchronizable {

  /**
   * an unmodifiable list of {@link ZKLock}s to be acquired in order.
   */
  private final List _locks;

  public MultiLocks(ZKClient zkClient, String... lockPaths)  {
    List locks = Arrays.stream(lockPaths)
        .sorted()
        .map(lockPath -> new ZKLock(lockPath, zkClient))
        .collect(Collectors.toList());
    _locks = Collections.unmodifiableList(locks);
  }

  /**
   * {@inheritDoc}
   */
  @Override
  public  Task synchronize(Task task, long deadline) {
    for (ZKLock lock : _locks) {
      task = lock.synchronize(task, deadline);
    }
    return task;
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy