Search in sources :

Example 1 with Lease

use of org.apache.curator.framework.recipes.locks.Lease in project fabric8 by jboss-fuse.

the class ZookeeperPortService method registerPort.

private void registerPort(Container container, String pid, String key, int port, Lease existingLease) {
    assertValid();
    String portAsString = String.valueOf(port);
    String containerPortsPath = ZkPath.PORTS_CONTAINER_PID_KEY.getPath(container.getId(), pid, key);
    String reservedPortsPath = ZkPath.PORTS_CONTAINER_RESERVED_PORTS.getPath(container.getId());
    String ip = container.getIp();
    assertValidIp(container, ip);
    String ipPortsPath = ZkPath.PORTS_IP.getPath(ip);
    Lease lease = null;
    try {
        if (existingLease != null) {
            lease = existingLease;
        } else {
            lease = interProcessLock.acquire(60, TimeUnit.SECONDS);
        }
        if (lease != null) {
            createDefault(curator.get(), containerPortsPath, portAsString);
            createDefault(curator.get(), ipPortsPath, portAsString);
            setData(curator.get(), containerPortsPath, portAsString);
            String existingPorts = getStringData(curator.get(), ipPortsPath);
            if (!existingPorts.contains(portAsString)) {
                setData(curator.get(), ipPortsPath, existingPorts + " " + portAsString);
                createDefault(curator.get(), reservedPortsPath, portAsString);
                String reservedPortsPerContainer = getStringData(curator.get(), reservedPortsPath);
                if (!reservedPortsPerContainer.contains(portAsString)) {
                    setData(curator.get(), reservedPortsPath, reservedPortsPerContainer + " " + portAsString);
                }
            }
        } else {
            throw new FabricException("Could not acquire port lock for pid " + pid);
        }
    } catch (InterruptedException ex) {
        cleanUpDirtyZKNodes(interProcessLock);
        throw FabricException.launderThrowable(ex);
    } catch (Exception ex) {
        throw FabricException.launderThrowable(ex);
    } finally {
        if (existingLease == null) {
            releaseLock(lease);
        }
    }
}
Also used : Lease(org.apache.curator.framework.recipes.locks.Lease) FabricException(io.fabric8.api.FabricException) FabricException(io.fabric8.api.FabricException)

Example 2 with Lease

use of org.apache.curator.framework.recipes.locks.Lease in project fabric8 by jboss-fuse.

the class ZookeeperPortService method registerPort.

@Override
public int registerPort(Container container, String pid, String key, int fromPort, int toPort, Set<Integer> excludes) {
    assertValid();
    Lease lease = null;
    try {
        lease = interProcessLock.acquire(60, TimeUnit.SECONDS);
        if (lease != null) {
            int port = lookupPort(container, pid, key);
            if (port > 0 && (port >= fromPort && port <= toPort)) {
                // get one from the port range
                return port;
            }
            Set<Integer> boundPorts = findUsedPortByHost(container, lease);
            boundPorts.addAll(excludes);
            for (port = fromPort; port <= toPort; port++) {
                if (!boundPorts.contains(port)) {
                    if (Ports.isPortFree(port)) {
                        registerPort(container, pid, key, port, lease);
                        return port;
                    }
                }
            }
        } else {
            throw new FabricException("Could not acquire port lock for pid " + pid);
        }
        throw new FabricException("Could not find port within range [" + fromPort + "," + toPort + "] for pid " + pid);
    } catch (InterruptedException ex) {
        cleanUpDirtyZKNodes(interProcessLock);
        throw FabricException.launderThrowable(ex);
    } catch (Exception ex) {
        throw FabricException.launderThrowable(ex);
    } finally {
        releaseLock(lease);
    }
}
Also used : Lease(org.apache.curator.framework.recipes.locks.Lease) FabricException(io.fabric8.api.FabricException) FabricException(io.fabric8.api.FabricException)

Example 3 with Lease

use of org.apache.curator.framework.recipes.locks.Lease in project fabric8 by jboss-fuse.

the class ZookeeperPortService method findUsedPortByHost.

private Set<Integer> findUsedPortByHost(Container container, Lease existingLease) {
    assertValid();
    String ip = container.getIp();
    assertValidIp(container, ip);
    Set<Integer> ports = new HashSet<Integer>();
    String path = ZkPath.PORTS_IP.getPath(ip);
    Lease lease = null;
    try {
        if (existingLease != null) {
            lease = existingLease;
        } else {
            lease = interProcessLock.acquire(60, TimeUnit.SECONDS);
        }
        if (lease != null) {
            createDefault(curator.get(), path, "");
            String boundPorts = getStringData(curator.get(), path);
            if (boundPorts != null && !boundPorts.isEmpty()) {
                for (String port : boundPorts.split(" ")) {
                    try {
                        ports.add(Integer.parseInt(port.trim()));
                    } catch (NumberFormatException ex) {
                    // ignore
                    }
                }
            }
        } else {
            throw new FabricException("Could not acquire port lock");
        }
    } catch (InterruptedException ex) {
        cleanUpDirtyZKNodes(interProcessLock);
        throw FabricException.launderThrowable(ex);
    } catch (Exception ex) {
        throw FabricException.launderThrowable(ex);
    } finally {
        if (existingLease == null) {
            releaseLock(lease);
        }
    }
    return ports;
}
Also used : Lease(org.apache.curator.framework.recipes.locks.Lease) FabricException(io.fabric8.api.FabricException) FabricException(io.fabric8.api.FabricException) HashSet(java.util.HashSet)

Example 4 with Lease

use of org.apache.curator.framework.recipes.locks.Lease in project fabric8 by jboss-fuse.

the class ZookeeperPortService method findUsedPortByContainer.

@Override
public Set<Integer> findUsedPortByContainer(Container container) {
    assertValid();
    Set<Integer> ports = new HashSet<Integer>();
    String path = ZkPath.PORTS_CONTAINER.getPath(container.getId());
    Lease lease = null;
    try {
        lease = interProcessLock.acquire(60, TimeUnit.SECONDS);
        if (lease != null) {
            if (exists(curator.get(), path) != null) {
                for (String pid : getChildren(curator.get(), path)) {
                    for (String key : getChildren(curator.get(), ZkPath.PORTS_CONTAINER_PID.getPath(container.getId(), pid))) {
                        String port = getStringData(curator.get(), ZkPath.PORTS_CONTAINER_PID_KEY.getPath(container.getId(), pid, key));
                        try {
                            ports.add(Integer.parseInt(port));
                        } catch (Exception ex) {
                        // ignore
                        }
                    }
                }
            }
        } else {
            throw new FabricException("Could not acquire port lock");
        }
    } catch (InterruptedException ex) {
        cleanUpDirtyZKNodes(interProcessLock);
        throw FabricException.launderThrowable(ex);
    } catch (Exception ex) {
        throw FabricException.launderThrowable(ex);
    } finally {
        releaseLock(lease);
    }
    return ports;
}
Also used : Lease(org.apache.curator.framework.recipes.locks.Lease) FabricException(io.fabric8.api.FabricException) FabricException(io.fabric8.api.FabricException) HashSet(java.util.HashSet)

Example 5 with Lease

use of org.apache.curator.framework.recipes.locks.Lease in project fabric8 by jboss-fuse.

the class ZookeeperPortService method unregisterPort.

@Override
public void unregisterPort(Container container) {
    assertValid();
    String containerPortsPath = ZkPath.PORTS_CONTAINER.getPath(container.getId());
    String reservedPortsPath = ZkPath.PORTS_CONTAINER_RESERVED_PORTS.getPath(container.getId());
    String ip = container.getIp();
    String ipPortsPath = ZkPath.PORTS_IP.getPath(ip);
    Lease lease = null;
    try {
        lease = interProcessLock.acquire(60, TimeUnit.SECONDS);
        if (lease != null) {
            if (exists(curator.get(), containerPortsPath) != null) {
                for (String pid : getChildren(curator.get(), containerPortsPath)) {
                    unregisterPort(container, pid, lease);
                    if (exists(curator.get(), reservedPortsPath) != null) {
                        Set<Integer> allPorts = findUsedPortByHost(container, lease);
                        String reservedPortsPerContainer = getStringData(curator.get(), reservedPortsPath);
                        String[] split = reservedPortsPerContainer.split(" ");
                        for (String p : split) {
                            allPorts.remove(Integer.valueOf(p));
                        }
                        StringBuilder sb = buildPortsString(allPorts);
                        setData(curator.get(), ipPortsPath, sb.toString());
                    }
                }
                deleteSafe(curator.get(), reservedPortsPath);
                deleteSafe(curator.get(), containerPortsPath);
            }
        } else {
            throw new FabricException("Could not acquire port lock");
        }
    } catch (InterruptedException ex) {
        cleanUpDirtyZKNodes(interProcessLock);
        throw FabricException.launderThrowable(ex);
    } catch (Exception ex) {
        throw FabricException.launderThrowable(ex);
    } finally {
        releaseLock(lease);
    }
}
Also used : Lease(org.apache.curator.framework.recipes.locks.Lease) FabricException(io.fabric8.api.FabricException) FabricException(io.fabric8.api.FabricException)

Aggregations

Lease (org.apache.curator.framework.recipes.locks.Lease)10 FabricException (io.fabric8.api.FabricException)6 HashSet (java.util.HashSet)2 Random (java.util.Random)2 Test (org.junit.Test)2 DatabaseException (com.emc.storageos.db.exceptions.DatabaseException)1 CIMInstance (javax.cim.CIMInstance)1 WBEMException (javax.wbem.WBEMException)1