public List<String> getSetPortUsedBySupervisor(String supervisorHost, int slotCount) throws Exception {

        String appPath = RegistryUtils.serviceclassPath(
                JOYConstants.APP_TYPE, JOYConstants.EMPTY);

        String path = RegistryUtils.serviceclassPath(
                JOYConstants.APP_TYPE, instanceName);

        String containerPath = RegistryUtils.componentPath(
                JOYConstants.APP_TYPE, instanceName, containerId.getApplicationAttemptId().getApplicationId().toString(), containerId.toString());

        List<String> reList = new ArrayList<String>();
        try {

            List<ServiceRecord> hostContainers = new ArrayList<ServiceRecord>();
            Set<String> hostUsedPorts = new HashSet<String>();


            List<String> instanceNames = registryOperations.list(appPath);
            for (String instance : instanceNames) {

                String servicePath = RegistryUtils.serviceclassPath(
                        JOYConstants.APP_TYPE, instance);

                List<String> apps = registryOperations.list(servicePath);

                for (String subapp : apps) {

                    String subAppPath = RegistryUtils.servicePath(
                            JOYConstants.APP_TYPE, instance, subapp);
                    String componentsPath = subAppPath + JOYConstants.COMPONENTS;
                    if (!registryOperations.exists(componentsPath))
                        continue;
                    Map<String, ServiceRecord> containers = RegistryUtils.listServiceRecords(registryOperations, componentsPath);

                    for (String container : containers.keySet()) {
                        ServiceRecord sr = containers.get(container);
                        LOG.info(sr.toString());

                        if (!sr.get(JOYConstants.HOST).equals(supervisorHost))
                            continue;
                        hostContainers.add(sr);
                        String[] portList = new String[]{};
                        if (sr.get(JOYConstants.PORT_LIST) != null)
                            portList = sr.get(JOYConstants.PORT_LIST).split(JOYConstants.COMMA);
                        for (String usedport : portList) {
                            hostUsedPorts.add(usedport);
                        }
                    }
                }
            }

            //scan port range from 9000 to 15000
            for (int i = getMinPort(); i < getMaxPort(); i++) {
                if (JstormYarnUtils.isPortAvailable(supervisorHost, i)) {
                    if (!hostUsedPorts.contains(String.valueOf(i)))
                        reList.add(String.valueOf(i));
                }
                if (reList.size() >= slotCount) {
                    break;
                }
            }


            if (registryOperations.exists(containerPath)) {
                ServiceRecord sr = registryOperations.resolve(containerPath);
                String portListUpdate = JstormYarnUtils.join(reList, JOYConstants.COMMA, false);
                if (sr.get(JOYConstants.PORT_LIST) != null) {
                    String[] portList = sr.get(JOYConstants.PORT_LIST).split(JOYConstants.COMMA);
                    portListUpdate = JstormYarnUtils.join(portList, JOYConstants.COMMA, true) + JstormYarnUtils.join(reList, JOYConstants.COMMA, false);
                }
                sr.set(JOYConstants.PORT_LIST, portListUpdate);
                registryOperations.bind(containerPath, sr, BindFlags.OVERWRITE);
            } else {
                registryOperations.mknode(containerPath, true);
                ServiceRecord sr = new ServiceRecord();
                sr.set(JOYConstants.HOST, supervisorHost);
                String portListUpdate = JstormYarnUtils.join(reList, JOYConstants.COMMA, false);
                sr.set(JOYConstants.PORT_LIST, portListUpdate);
                sr.set(YarnRegistryAttributes.YARN_ID, containerId.toString());
                sr.description = JOYConstants.CONTAINER;
                sr.set(YarnRegistryAttributes.YARN_PERSISTENCE,
                        PersistencePolicies.CONTAINER);
                registryOperations.bind(containerPath, sr, BindFlags.OVERWRITE);
            }
            return reList;
        } catch (Exception ex) {
            LOG.error(ex);
            throw ex;
        }
    }
public List < String > getSetPortUsedBySupervisor ( String supervisorHost , int slotCount ) throws Exception { String appPath = RegistryUtils . serviceclassPath ( JOYConstants . APP_TYPE , JOYConstants . EMPTY ) ; String path = RegistryUtils . serviceclassPath ( JOYConstants . APP_TYPE , instanceName ) ; String containerPath = RegistryUtils . componentPath ( JOYConstants . APP_TYPE , instanceName , containerId . getApplicationAttemptId ( ) . getApplicationId ( ) . toString ( ) , containerId . toString ( ) ) ; List < String > reList = new ArrayList < String > ( ) ; try { List < ServiceRecord > hostContainers = new ArrayList < ServiceRecord > ( ) ; Set < String > hostUsedPorts = new HashSet < String > ( ) ; List < String > instanceNames = registryOperations . list ( appPath ) ; for ( String instance : instanceNames ) { String servicePath = RegistryUtils . serviceclassPath ( JOYConstants . APP_TYPE , instance ) ; List < String > apps = registryOperations . list ( servicePath ) ; for ( String subapp : apps ) { String subAppPath = RegistryUtils . servicePath ( JOYConstants . APP_TYPE , instance , subapp ) ; String componentsPath = subAppPath + JOYConstants . COMPONENTS ; if ( ! registryOperations . exists ( componentsPath ) ) continue ; Map < String , ServiceRecord > containers = RegistryUtils . listServiceRecords ( registryOperations , componentsPath ) ; for ( String container : containers . keySet ( ) ) { ServiceRecord sr = containers . get ( container ) ; LOG . info ( sr . toString ( ) ) ; if ( ! sr . get ( JOYConstants . HOST ) . equals ( supervisorHost ) ) continue ; hostContainers . add ( sr ) ; String [ ] portList = new String [ ] { } ; if ( sr . get ( JOYConstants . PORT_LIST ) != null ) portList = sr . get ( JOYConstants . PORT_LIST ) . split ( JOYConstants . COMMA ) ; for ( String usedport : portList ) { hostUsedPorts . add ( usedport ) ; } } } } for ( int i = getMinPort ( ) ; i < getMaxPort ( ) ; i ++ ) { if ( JstormYarnUtils . isPortAvailable ( supervisorHost , i ) ) { if ( ! hostUsedPorts . contains ( String . valueOf ( i ) ) ) reList . add ( String . valueOf ( i ) ) ; } if ( reList . size ( ) >= slotCount ) { break ; } } if ( registryOperations . exists ( containerPath ) ) { ServiceRecord sr = registryOperations . resolve ( containerPath ) ; String portListUpdate = JstormYarnUtils . join ( reList , JOYConstants . COMMA , false ) ; if ( sr . get ( JOYConstants . PORT_LIST ) != null ) { String [ ] portList = sr . get ( JOYConstants . PORT_LIST ) . split ( JOYConstants . COMMA ) ; portListUpdate = JstormYarnUtils . join ( portList , JOYConstants . COMMA , true ) + JstormYarnUtils . join ( reList , JOYConstants . COMMA , false ) ; } sr . set ( JOYConstants . PORT_LIST , portListUpdate ) ; registryOperations . bind ( containerPath , sr , BindFlags . OVERWRITE ) ; } else { registryOperations . mknode ( containerPath , true ) ; ServiceRecord sr = new ServiceRecord ( ) ; sr . set ( JOYConstants . HOST , supervisorHost ) ; String portListUpdate = JstormYarnUtils . join ( reList , JOYConstants . COMMA , false ) ; sr . set ( JOYConstants . PORT_LIST , portListUpdate ) ; sr . set ( YarnRegistryAttributes . YARN_ID , containerId . toString ( ) ) ; sr . description = JOYConstants . CONTAINER ; sr . set ( YarnRegistryAttributes . YARN_PERSISTENCE , PersistencePolicies . CONTAINER ) ; registryOperations . bind ( containerPath , sr , BindFlags . OVERWRITE ) ; } return reList ; } catch ( Exception ex ) { LOG . error ( ex ) ; throw ex ; } }
see if port is using by supervisor
if used return true, if not, add this port to registry and return false

@param supervisorHost
@param slotCount
@return