Example usage for org.joda.time Duration standardSeconds

List of usage examples for org.joda.time Duration standardSeconds

Introduction

In this page you can find the example usage for org.joda.time Duration standardSeconds.

Prototype

public static Duration standardSeconds(long seconds) 

Source Link

Document

Create a duration with the specified number of seconds assuming that there are the standard number of milliseconds in a second.

Usage

From source file:com.aliakseipilko.flightdutytracker.presenter.impl.HourPresenter.java

License:Open Source License

@Override
public void subscribeAllCallbacks() {
    getMultipleFlightsDutyHoursCallback = new IFlightRepository.OnGetMultipleFlightsCallback() {
        @Override/*from  w w  w .j av  a 2 s .  c  om*/
        public void OnSuccess(RealmResults<Flight> flights) {
            double dutyMillis = 0;

            for (Flight flight : flights) {
                dutyMillis += (flight.getEndDutyTime().getTime() - flight.getStartDutyTime().getTime());
            }

            Duration dutyDuration = Duration.millis((long) dutyMillis);
            long hours = dutyDuration.getStandardHours();
            long minutes = dutyDuration.minus(hours * 60 * 60 * 1000).getStandardMinutes();
            if (minutes < 10) {
                view.showDutyHours(hours + ":0" + minutes, dutyMillis);
            } else {
                view.showDutyHours(hours + ":" + minutes, dutyMillis);
            }
        }

        @Override
        public void OnError(String message) {
            view.showError(message);
        }
    };
    getMultipleFlightsFlightHoursCallback = new IFlightRepository.OnGetMultipleFlightsCallback() {
        @Override
        public void OnSuccess(RealmResults<Flight> flights) {
            double flightMillis = 0;

            for (Flight flight : flights) {
                flightMillis += (flight.getEndFlightTime().getTime() - flight.getStartFlightTime().getTime());
            }

            Duration dutyDuration = Duration.standardSeconds((long) (flightMillis / 1000));
            long hours = dutyDuration.getStandardHours();
            long minutes = dutyDuration.minus(hours * 60 * 60 * 1000).getStandardMinutes();
            if (minutes < 10) {
                view.showFlightHours(hours + ":0" + minutes, flightMillis);
            } else {
                view.showFlightHours(hours + ":" + minutes, flightMillis);
            }
        }

        @Override
        public void OnError(String message) {
            view.showError(message);
        }
    };
}

From source file:com.arpnetworking.tsdcore.sinks.VertxSink.java

License:Apache License

private void consumeLoop() {
    long flushedBytes = 0;
    try {/*  ww w.j  a v a2 s .c  om*/
        boolean done = false;
        NetSocket socket = _socket.get();
        if (!_pendingData.isEmpty()) {
            LOGGER.debug().setMessage("Pending data").addData("sink", getName())
                    .addData("size", _pendingData.size()).log();
        }
        while (socket != null && !done) {
            if (_pendingData.size() > 0 && flushedBytes < MAX_FLUSH_BYTES) {
                final Buffer buffer = _pendingData.poll();
                flushedBytes += flushBuffer(buffer, socket);
            } else {
                done = true;
            }
            socket = _socket.get();
        }
        if (socket == null && (_lastNotConnectedNotify == null
                || _lastNotConnectedNotify.plus(Duration.standardSeconds(30)).isBeforeNow())) {
            LOGGER.debug().setMessage("Not connected to server. Data will be flushed when reconnected. "
                    + "Suppressing this message for 30 seconds.").addData("sink", getName()).log();
            _lastNotConnectedNotify = DateTime.now();
        }
        // CHECKSTYLE.OFF: IllegalCatch - Vertx might not log
    } catch (final Exception e) {
        // CHECKSTYLE.ON: IllegalCatch
        LOGGER.error().setMessage("Error in consume loop").addData("sink", getName()).setThrowable(e).log();
        throw e;
    } finally {
        if (flushedBytes > 0) {
            dispatch(event -> consumeLoop());
        } else {
            getVertx().setTimer(NO_DATA_CONSUME_LOOP_INTERVAL, event -> consumeLoop());
        }
    }
}

From source file:com.boundary.metrics.vmware.poller.VMwarePerfPoller.java

License:Apache License

/**
 * Extracts performance metrics from Managed Objects on the monitored entity
 * //  w  w w.  j av a 2  s  .c  o m
 * @throws MalformedURLException Bad URL
 * @throws RemoteException Endpoint exception
 * @throws InvalidPropertyFaultMsg Bad Property
 * @throws RuntimeFaultFaultMsg Runtime error
 * @throws SOAPFaultException WebServer error
 */
public void collectPerformanceData() throws MalformedURLException, RemoteException, InvalidPropertyFaultMsg,
        RuntimeFaultFaultMsg, SOAPFaultException {

    ManagedObjectReference root = client.getServiceContent().getRootFolder();

    // 'now' according to the server
    DateTime now = TimeUtils.toDateTime(client.getVimPort().currentTime(client.getServiceInstanceReference()));
    Duration serverSkew = new Duration(now, new DateTime());
    if (serverSkew.isLongerThan(Duration.standardSeconds(1))
            && (skew == null || skew.getStandardSeconds() != serverSkew.getStandardSeconds())) {
        LOG.warn("Server {} and local time skewed by {} seconds", client.getHost(),
                serverSkew.getStandardSeconds());
        skew = serverSkew;
    }
    if (lastPoll == null) {
        lastPoll = now.minusSeconds(20);
    }

    // Holder for all our newly found measurements
    // TODO set an upper size limit on measurements list
    List<Measurement> measurements = Lists.newArrayList();

    /*
    * A {@link PerfMetricId} consistents of the performance counter and
    * the instance it applies to.
    * 
    * In our particular case we are requesting for all of the instances
    * associated with the performance counter.
    * 
    * Will this work when we have a mix of VirtualMachine, HostSystem, and DataSource
    * managed objects.
    * 
    */
    List<PerfMetricId> perfMetricIds = Lists.newArrayList();
    for (String counterName : metrics.keySet()) {
        if (this.performanceCounterMap.containsKey(counterName)) {
            PerfMetricId metricId = new PerfMetricId();
            /* Get the ID for this counter. */
            metricId.setCounterId(this.performanceCounterMap.get(counterName));
            metricId.setInstance("*");
            perfMetricIds.add(metricId);
        }
    }

    GetMOREF getMOREFs = new GetMOREF(client);
    Map<String, ManagedObjectReference> entities = getMOREFs.inFolderByType(root, "VirtualMachine");

    for (Map.Entry<String, ManagedObjectReference> entity : entities.entrySet()) {
        ManagedObjectReference mor = entity.getValue();
        String entityName = entity.getKey();

        /*
        * Create the query specification for queryPerf().
        * Specify 5 minute rollup interval and CSV output format.
        */
        PerfQuerySpec querySpec = new PerfQuerySpec();
        querySpec.setEntity(mor);
        querySpec.setIntervalId(20);
        querySpec.setFormat("normal");
        querySpec.setStartTime(TimeUtils.toXMLGregorianCalendar(lastPoll));
        querySpec.setEndTime(TimeUtils.toXMLGregorianCalendar(now));
        querySpec.getMetricId().addAll(perfMetricIds);

        LOG.info("Entity: {}, MOR: {}-{}, Interval: {}, Format: {}, MetricIds: {}, Start: {}, End: {}",
                entityName, mor.getType(), mor.getValue(), querySpec.getIntervalId(), querySpec.getFormat(),
                FluentIterable.from(perfMetricIds).transform(toStringFunction), lastPoll, now);

        List<PerfEntityMetricBase> retrievedStats = client.getVimPort()
                .queryPerf(client.getServiceContent().getPerfManager(), ImmutableList.of(querySpec));

        /*
        * Cycle through the PerfEntityMetricBase objects. Each object contains
        * a set of statistics for a single ManagedEntity.
        */
        for (PerfEntityMetricBase singleEntityPerfStats : retrievedStats) {
            if (singleEntityPerfStats instanceof PerfEntityMetric) {
                PerfEntityMetric entityStats = (PerfEntityMetric) singleEntityPerfStats;
                List<PerfMetricSeries> metricValues = entityStats.getValue();
                List<PerfSampleInfo> sampleInfos = entityStats.getSampleInfo();

                for (int x = 0; x < metricValues.size(); x++) {
                    PerfMetricIntSeries metricReading = (PerfMetricIntSeries) metricValues.get(x);
                    PerfCounterInfo metricInfo = performanceCounterInfoMap
                            .get(metricReading.getId().getCounterId());
                    String metricFullName = toFullName.apply(metricInfo);
                    if (!sampleInfos.isEmpty()) {
                        PerfSampleInfo sampleInfo = sampleInfos.get(0);
                        DateTime sampleTime = TimeUtils.toDateTime(sampleInfo.getTimestamp());
                        Number sampleValue = metricReading.getValue().iterator().next();

                        if (skew != null) {
                            sampleTime = sampleTime.plusSeconds((int) skew.getStandardSeconds());
                        }

                        if (metricReading.getValue().size() > 1) {
                            LOG.warn("Metric {} has more than one value, only using the first", metricFullName);
                        }

                        String source = client.getName() + "-" + entityName;

                        if (metricInfo.getUnitInfo().getKey().equalsIgnoreCase("kiloBytes")) {
                            sampleValue = (long) sampleValue * 1024; // Convert KB to Bytes
                        } else if (metricInfo.getUnitInfo().getKey().equalsIgnoreCase("percent")) {
                            // Convert hundredth of a percent to a decimal percent
                            sampleValue = new Long((long) sampleValue).doubleValue() / 10000.0;
                        }
                        String name = metrics.get(metricFullName).getName();
                        if (name != null) {
                            Measurement measurement = Measurement.builder().setMetric(name).setSource(source)
                                    .setTimestamp(sampleTime).setMeasurement(sampleValue).build();
                            measurements.add(measurement);

                            LOG.info("{} @ {} = {} {}", metricFullName, sampleTime, sampleValue,
                                    metricInfo.getUnitInfo().getKey());
                        } else {
                            LOG.warn("Skipping collection of metric: {}", metricFullName);
                        }
                    } else {
                        LOG.warn("Didn't receive any samples when polling for {} on {} between {} and {}",
                                metricFullName, client.getHost(), lastPoll, now);
                    }
                }
            } else {
                LOG.error("Unrecognized performance entry type received: {}, ignoring",
                        singleEntityPerfStats.getClass().getName());
            }
        }
    }

    // Send metrics
    if (!measurements.isEmpty()) {
        metricsClient.addMeasurements(measurements);
    } else {
        LOG.warn("No measurements collected in last poll for {}", client.getHost());
    }

    // Reset lastPoll time
    lastPoll = now;
}

From source file:com.brighttag.agathon.dao.zerg.ZergDaoModule.java

License:Apache License

@Override
protected void configure() {
    bind(Gson.class).in(Singleton.class);
    bindConstant().annotatedWith(Names.named(ZERG_MANIFEST_URL_PROPERTY))
            .to(System.getProperty(ZERG_MANIFEST_URL_PROPERTY, ZERG_MANIFEST_URL_DEFAULT));
    bindConstant().annotatedWith(Names.named(ZERG_REGION_PROPERTY))
            .to(checkNotNull(System.getProperty(ZERG_REGION_PROPERTY), "Zerg region not specified"));
    bindConstant().annotatedWith(Names.named(ZERG_RING_CONFIG_PROPERTY)).to(
            checkNotNull(System.getProperty(ZERG_RING_CONFIG_PROPERTY), "Zerg ring config file not specified"));
    // Yes, Zerg is THIS SLOW when the manifest isn't cached, especially in AWS.
    bind(Duration.class).annotatedWith(Names.named(ZERG_REQUEST_TIMEOUT_PROPERTY))
            .toInstance(Duration.standardSeconds(Long.getLong(ZERG_REQUEST_TIMEOUT_PROPERTY, 20)));
    bind(Duration.class).annotatedWith(Names.named(ZERG_CONNECTION_TIMEOUT_PROPERTY))
            .toInstance(Duration.standardSeconds(Long.getLong(ZERG_CONNECTION_TIMEOUT_PROPERTY, 20)));
    bind(Duration.class).annotatedWith(Names.named(ZERG_CACHE_TIMEOUT_PROPERTY))
            .toInstance(Duration.standardMinutes(Long.getLong(ZERG_CACHE_TIMEOUT_PROPERTY, 1)));
    bind(ZergConnector.class).to(ZergConnectorImpl.class).in(Singleton.class);
    bind(CassandraRingDao.class).to(ZergCassandraRingDao.class).in(Singleton.class);
    bind(CassandraInstanceDao.class).to(ZergCassandraInstanceDao.class).in(Singleton.class);
    expose(CassandraRingDao.class);
    expose(CassandraInstanceDao.class);
}

From source file:com.cloud.agent.resource.virtualnetwork.VirtualRoutingResource.java

License:Apache License

public boolean configureHostParams(final Map<String, String> params) {
    if (_params.get("router.aggregation.command.each.timeout") == null) {
        String value = (String) params.get("router.aggregation.command.each.timeout");
        _eachTimeout = Duration.standardSeconds(NumbersUtil.parseInt(value, 10));
    }//from  w w  w  . jav  a  2s . com

    return true;
}

From source file:com.cloud.agent.resource.virtualnetwork.VirtualRoutingResource.java

License:Apache License

public boolean configure(final String name, final Map<String, Object> params) throws ConfigurationException {
    _name = name;/*from www . j a  v  a  2 s . co m*/
    _params = params;

    String value = (String) params.get("ssh.sleep");
    _sleep = NumbersUtil.parseInt(value, 10) * 1000;

    value = (String) params.get("ssh.retry");
    _retry = NumbersUtil.parseInt(value, 36);

    value = (String) params.get("ssh.port");
    _port = NumbersUtil.parseInt(value, 3922);

    value = (String) params.get("router.aggregation.command.each.timeout");
    _eachTimeout = Duration.standardSeconds(
            NumbersUtil.parseInt(value, (int) VRScripts.VR_SCRIPT_EXEC_TIMEOUT.getStandardSeconds()));
    if (s_logger.isDebugEnabled()) {
        s_logger.debug("The router.aggregation.command.each.timeout in seconds is set to "
                + _eachTimeout.getStandardSeconds());
    }

    if (_vrDeployer == null) {
        throw new ConfigurationException("Unable to find the resource for VirtualRouterDeployer!");
    }

    _vrAggregateCommandsSet = new HashMap<>();
    return true;
}

From source file:com.cloud.hypervisor.hyperv.resource.HypervDirectConnectResource.java

License:Apache License

@Override
public ExecutionResult executeInVR(final String routerIP, final String script, final String args) {
    return executeInVR(routerIP, script, args, Duration.standardSeconds(120L));
}

From source file:com.cloud.hypervisor.kvm.resource.LibvirtComputingResource.java

License:Apache License

@Override
public boolean configure(final String name, final Map<String, Object> params) throws ConfigurationException {
    boolean success = super.configure(name, params);
    if (!success) {
        return false;
    }/*w ww .  ja  v a 2 s. co  m*/

    _storage = new JavaStorageLayer();
    _storage.configure("StorageLayer", params);

    String domrScriptsDir = (String) params.get("domr.scripts.dir");
    if (domrScriptsDir == null) {
        domrScriptsDir = getDefaultDomrScriptsDir();
    }

    String hypervisorScriptsDir = (String) params.get("hypervisor.scripts.dir");
    if (hypervisorScriptsDir == null) {
        hypervisorScriptsDir = getDefaultHypervisorScriptsDir();
    }

    String kvmScriptsDir = (String) params.get("kvm.scripts.dir");
    if (kvmScriptsDir == null) {
        kvmScriptsDir = getDefaultKvmScriptsDir();
    }

    String networkScriptsDir = (String) params.get("network.scripts.dir");
    if (networkScriptsDir == null) {
        networkScriptsDir = getDefaultNetworkScriptsDir();
    }

    String storageScriptsDir = (String) params.get("storage.scripts.dir");
    if (storageScriptsDir == null) {
        storageScriptsDir = getDefaultStorageScriptsDir();
    }

    final String bridgeType = (String) params.get("network.bridge.type");
    if (bridgeType == null) {
        _bridgeType = BridgeType.NATIVE;
    } else {
        _bridgeType = BridgeType.valueOf(bridgeType.toUpperCase());
    }

    params.put("domr.scripts.dir", domrScriptsDir);

    _virtRouterResource = new VirtualRoutingResource(this);
    success = _virtRouterResource.configure(name, params);

    if (!success) {
        return false;
    }

    _host = (String) params.get("host");
    if (_host == null) {
        _host = "localhost";
    }

    _dcId = (String) params.get("zone");
    if (_dcId == null) {
        _dcId = "default";
    }

    _pod = (String) params.get("pod");
    if (_pod == null) {
        _pod = "default";
    }

    _clusterId = (String) params.get("cluster");

    _updateHostPasswdPath = Script.findScript(hypervisorScriptsDir, VRScripts.UPDATE_HOST_PASSWD);
    if (_updateHostPasswdPath == null) {
        throw new ConfigurationException("Unable to find update_host_passwd.sh");
    }

    _modifyVlanPath = Script.findScript(networkScriptsDir, "modifyvlan.sh");
    if (_modifyVlanPath == null) {
        throw new ConfigurationException("Unable to find modifyvlan.sh");
    }

    _versionstringpath = Script.findScript(kvmScriptsDir, "versions.sh");
    if (_versionstringpath == null) {
        throw new ConfigurationException("Unable to find versions.sh");
    }

    _patchViaSocketPath = Script.findScript(kvmScriptsDir + "/patch/", "patchviasocket.py");
    if (_patchViaSocketPath == null) {
        throw new ConfigurationException("Unable to find patchviasocket.py");
    }

    _heartBeatPath = Script.findScript(kvmScriptsDir, "kvmheartbeat.sh");
    if (_heartBeatPath == null) {
        throw new ConfigurationException("Unable to find kvmheartbeat.sh");
    }

    _createvmPath = Script.findScript(storageScriptsDir, "createvm.sh");
    if (_createvmPath == null) {
        throw new ConfigurationException("Unable to find the createvm.sh");
    }

    _manageSnapshotPath = Script.findScript(storageScriptsDir, "managesnapshot.sh");
    if (_manageSnapshotPath == null) {
        throw new ConfigurationException("Unable to find the managesnapshot.sh");
    }

    _resizeVolumePath = Script.findScript(storageScriptsDir, "resizevolume.sh");
    if (_resizeVolumePath == null) {
        throw new ConfigurationException("Unable to find the resizevolume.sh");
    }

    _createTmplPath = Script.findScript(storageScriptsDir, "createtmplt.sh");
    if (_createTmplPath == null) {
        throw new ConfigurationException("Unable to find the createtmplt.sh");
    }

    _securityGroupPath = Script.findScript(networkScriptsDir, "security_group.py");
    if (_securityGroupPath == null) {
        throw new ConfigurationException("Unable to find the security_group.py");
    }

    _ovsTunnelPath = Script.findScript(networkScriptsDir, "ovstunnel.py");
    if (_ovsTunnelPath == null) {
        throw new ConfigurationException("Unable to find the ovstunnel.py");
    }

    _routerProxyPath = Script.findScript("scripts/network/domr/", "router_proxy.sh");
    if (_routerProxyPath == null) {
        throw new ConfigurationException("Unable to find the router_proxy.sh");
    }

    _ovsPvlanDhcpHostPath = Script.findScript(networkScriptsDir, "ovs-pvlan-dhcp-host.sh");
    if (_ovsPvlanDhcpHostPath == null) {
        throw new ConfigurationException("Unable to find the ovs-pvlan-dhcp-host.sh");
    }

    _ovsPvlanVmPath = Script.findScript(networkScriptsDir, "ovs-pvlan-vm.sh");
    if (_ovsPvlanVmPath == null) {
        throw new ConfigurationException("Unable to find the ovs-pvlan-vm.sh");
    }

    String value = (String) params.get("developer");
    final boolean isDeveloper = Boolean.parseBoolean(value);

    if (isDeveloper) {
        params.putAll(getDeveloperProperties());
    }

    _pool = (String) params.get("pool");
    if (_pool == null) {
        _pool = "/root";
    }

    final String instance = (String) params.get("instance");

    _hypervisorType = HypervisorType.getType((String) params.get("hypervisor.type"));
    if (_hypervisorType == HypervisorType.None) {
        _hypervisorType = HypervisorType.KVM;
    }

    _hypervisorURI = (String) params.get("hypervisor.uri");
    if (_hypervisorURI == null) {
        _hypervisorURI = LibvirtConnection.getHypervisorURI(_hypervisorType.toString());
    }

    _networkDirectSourceMode = (String) params.get("network.direct.source.mode");
    _networkDirectDevice = (String) params.get("network.direct.device");

    String startMac = (String) params.get("private.macaddr.start");
    if (startMac == null) {
        startMac = "00:16:3e:77:e2:a0";
    }

    String startIp = (String) params.get("private.ipaddr.start");
    if (startIp == null) {
        startIp = "192.168.166.128";
    }

    _pingTestPath = Script.findScript(kvmScriptsDir, "pingtest.sh");
    if (_pingTestPath == null) {
        throw new ConfigurationException("Unable to find the pingtest.sh");
    }

    _linkLocalBridgeName = (String) params.get("private.bridge.name");
    if (_linkLocalBridgeName == null) {
        if (isDeveloper) {
            _linkLocalBridgeName = "cloud-" + instance + "-0";
        } else {
            _linkLocalBridgeName = "cloud0";
        }
    }

    _publicBridgeName = (String) params.get("public.network.device");
    if (_publicBridgeName == null) {
        _publicBridgeName = "cloudbr0";
    }

    _privBridgeName = (String) params.get("private.network.device");
    if (_privBridgeName == null) {
        _privBridgeName = "cloudbr1";
    }

    _guestBridgeName = (String) params.get("guest.network.device");
    if (_guestBridgeName == null) {
        _guestBridgeName = _privBridgeName;
    }

    _privNwName = (String) params.get("private.network.name");
    if (_privNwName == null) {
        if (isDeveloper) {
            _privNwName = "cloud-" + instance + "-private";
        } else {
            _privNwName = "cloud-private";
        }
    }

    _localStoragePath = (String) params.get("local.storage.path");
    if (_localStoragePath == null) {
        _localStoragePath = "/var/lib/libvirt/images/";
    }

    /* Directory to use for Qemu sockets like for the Qemu Guest Agent */
    _qemuSocketsPath = new File("/var/lib/libvirt/qemu");
    String _qemuSocketsPathVar = (String) params.get("qemu.sockets.path");
    if (_qemuSocketsPathVar != null && StringUtils.isNotBlank(_qemuSocketsPathVar)) {
        _qemuSocketsPath = new File(_qemuSocketsPathVar);
    }

    final File storagePath = new File(_localStoragePath);
    _localStoragePath = storagePath.getAbsolutePath();

    _localStorageUUID = (String) params.get("local.storage.uuid");
    if (_localStorageUUID == null) {
        _localStorageUUID = UUID.randomUUID().toString();
    }

    value = (String) params.get("scripts.timeout");
    _timeout = Duration.standardSeconds(NumbersUtil.parseInt(value, 30 * 60));

    value = (String) params.get("stop.script.timeout");
    _stopTimeout = NumbersUtil.parseInt(value, 120) * 1000;

    value = (String) params.get("cmds.timeout");
    _cmdsTimeout = NumbersUtil.parseInt(value, 7200) * 1000;

    value = (String) params.get("vm.memballoon.disable");
    if (Boolean.parseBoolean(value)) {
        _noMemBalloon = true;
    }

    _videoHw = (String) params.get("vm.video.hardware");
    value = (String) params.get("vm.video.ram");
    _videoRam = NumbersUtil.parseInt(value, 0);

    value = (String) params.get("host.reserved.mem.mb");
    // Reserve 1GB unless admin overrides
    _dom0MinMem = NumbersUtil.parseInt(value, 1024) * 1024 * 1024L;

    value = (String) params.get("kvmclock.disable");
    if (Boolean.parseBoolean(value)) {
        _noKvmClock = true;
    }

    value = (String) params.get("vm.rng.enable");
    if (Boolean.parseBoolean(value)) {
        _rngEnable = true;

        value = (String) params.get("vm.rng.model");
        if (!Strings.isNullOrEmpty(value)) {
            _rngBackendModel = RngBackendModel.valueOf(value.toUpperCase());
        }

        value = (String) params.get("vm.rng.path");
        if (!Strings.isNullOrEmpty(value)) {
            _rngPath = value;
        }

        value = (String) params.get("vm.rng.rate.bytes");
        _rngRateBytes = NumbersUtil.parseInt(value, new Integer(_rngRateBytes));

        value = (String) params.get("vm.rng.rate.period");
        _rngRatePeriod = NumbersUtil.parseInt(value, new Integer(_rngRatePeriod));
    }

    LibvirtConnection.initialize(_hypervisorURI);
    Connect conn = null;
    try {
        conn = LibvirtConnection.getConnection();

        if (_bridgeType == BridgeType.OPENVSWITCH) {
            if (conn.getLibVirVersion() < 10 * 1000 + 0) {
                throw new ConfigurationException(
                        "Libvirt version 0.10.0 required for openvswitch support, but version "
                                + conn.getLibVirVersion() + " detected");
            }
        }
    } catch (final LibvirtException e) {
        throw new CloudRuntimeException(e.getMessage());
    }

    if (HypervisorType.KVM == _hypervisorType) {
        /* Does node support HVM guest? If not, exit */
        if (!IsHVMEnabled(conn)) {
            throw new ConfigurationException("NO HVM support on this machine, please make sure: "
                    + "1. VT/SVM is supported by your CPU, or is enabled in BIOS. "
                    + "2. kvm modules are loaded (kvm, kvm_amd|kvm_intel)");
        }
    }

    _hypervisorPath = getHypervisorPath(conn);
    try {
        _hvVersion = conn.getVersion();
        _hvVersion = _hvVersion % 1000000 / 1000;
        _hypervisorLibvirtVersion = conn.getLibVirVersion();
        _hypervisorQemuVersion = conn.getVersion();
    } catch (final LibvirtException e) {
        s_logger.trace("Ignoring libvirt error.", e);
    }

    _guestCpuMode = (String) params.get("guest.cpu.mode");
    if (_guestCpuMode != null) {
        _guestCpuModel = (String) params.get("guest.cpu.model");

        if (_hypervisorLibvirtVersion < 9 * 1000 + 10) {
            s_logger.warn("Libvirt version 0.9.10 required for guest cpu mode, but version "
                    + prettyVersion(_hypervisorLibvirtVersion) + " detected, so it will be disabled");
            _guestCpuMode = "";
            _guestCpuModel = "";
        }
        params.put("guest.cpu.mode", _guestCpuMode);
        params.put("guest.cpu.model", _guestCpuModel);
    }

    final String cpuFeatures = (String) params.get("guest.cpu.features");
    if (cpuFeatures != null) {
        _cpuFeatures = new ArrayList<String>();
        for (final String feature : cpuFeatures.split(" ")) {
            if (!feature.isEmpty()) {
                _cpuFeatures.add(feature);
            }
        }
    }

    final String[] info = NetUtils.getNetworkParams(_privateNic);

    _monitor = new KVMHAMonitor(null, info[0], _heartBeatPath);
    final Thread ha = new Thread(_monitor);
    ha.start();

    _storagePoolMgr = new KVMStoragePoolManager(_storage, _monitor);

    _sysvmISOPath = (String) params.get("systemvm.iso.path");
    if (_sysvmISOPath == null) {
        final String[] isoPaths = { "/usr/share/cloudstack-common/vms/systemvm.iso" };
        for (final String isoPath : isoPaths) {
            if (_storage.exists(isoPath)) {
                _sysvmISOPath = isoPath;
                break;
            }
        }
        if (_sysvmISOPath == null) {
            s_logger.debug("Can't find system vm ISO");
        }
    }

    switch (_bridgeType) {
    case OPENVSWITCH:
        getOvsPifs();
        break;
    case NATIVE:
    default:
        getPifs();
        break;
    }

    if (_pifs.get("private") == null) {
        s_logger.debug("Failed to get private nic name");
        throw new ConfigurationException("Failed to get private nic name");
    }

    if (_pifs.get("public") == null) {
        s_logger.debug("Failed to get public nic name");
        throw new ConfigurationException("Failed to get public nic name");
    }
    s_logger.debug("Found pif: " + _pifs.get("private") + " on " + _privBridgeName + ", pif: "
            + _pifs.get("public") + " on " + _publicBridgeName);

    _canBridgeFirewall = canBridgeFirewall(_pifs.get("public"));

    _localGateway = Script.runSimpleBashScript("ip route |grep default|awk '{print $3}'");
    if (_localGateway == null) {
        s_logger.debug("Failed to found the local gateway");
    }

    _mountPoint = (String) params.get("mount.path");
    if (_mountPoint == null) {
        _mountPoint = "/mnt";
    }

    value = (String) params.get("vm.migrate.downtime");
    _migrateDowntime = NumbersUtil.parseInt(value, -1);

    value = (String) params.get("vm.migrate.pauseafter");
    _migratePauseAfter = NumbersUtil.parseInt(value, -1);

    value = (String) params.get("vm.migrate.speed");
    _migrateSpeed = NumbersUtil.parseInt(value, -1);
    if (_migrateSpeed == -1) {
        //get guest network device speed
        _migrateSpeed = 0;
        final String speed = Script
                .runSimpleBashScript("ethtool " + _pifs.get("public") + " |grep Speed | cut -d \\  -f 2");
        if (speed != null) {
            final String[] tokens = speed.split("M");
            if (tokens.length == 2) {
                try {
                    _migrateSpeed = Integer.parseInt(tokens[0]);
                } catch (final NumberFormatException e) {
                    s_logger.trace("Ignoring migrateSpeed extraction error.", e);
                }
                s_logger.debug(
                        "device " + _pifs.get("public") + " has speed: " + String.valueOf(_migrateSpeed));
            }
        }
        params.put("vm.migrate.speed", String.valueOf(_migrateSpeed));
    }

    final Map<String, String> bridges = new HashMap<String, String>();
    bridges.put("linklocal", _linkLocalBridgeName);
    bridges.put("public", _publicBridgeName);
    bridges.put("private", _privBridgeName);
    bridges.put("guest", _guestBridgeName);

    params.put("libvirt.host.bridges", bridges);
    params.put("libvirt.host.pifs", _pifs);

    params.put("libvirt.computing.resource", this);
    params.put("libvirtVersion", _hypervisorLibvirtVersion);

    configureVifDrivers(params);
    configureDiskActivityChecks(params);

    final KVMStorageProcessor storageProcessor = new KVMStorageProcessor(_storagePoolMgr, this);
    storageProcessor.configure(name, params);
    storageHandler = new StorageSubsystemCommandHandlerBase(storageProcessor);

    return true;
}

From source file:com.dataartisans.flink.dataflow.examples.streaming.AutoComplete.java

License:Apache License

public static void main(String[] args) throws IOException {
    Options options = PipelineOptionsFactory.fromArgs(args).withValidation().as(Options.class);
    options.setStreaming(true);/*  w ww.j  ava 2 s . c  om*/
    options.setCheckpointingInterval(1000L);
    options.setNumberOfExecutionRetries(5);
    options.setExecutionRetryDelay(3000L);
    options.setRunner(FlinkPipelineRunner.class);

    PTransform<? super PBegin, PCollection<String>> readSource = Read
            .from(new UnboundedSocketSource<>("localhost", 9999, '\n', 3)).named("WordStream");
    WindowFn<Object, ?> windowFn = FixedWindows.of(Duration.standardSeconds(options.getWindowSize()));

    // Create the pipeline.
    Pipeline p = Pipeline.create(options);
    PCollection<KV<String, List<CompletionCandidate>>> toWrite = p.apply(readSource)
            .apply(ParDo.of(new ExtractWordsFn()))
            .apply(Window.<String>into(windowFn).triggering(AfterWatermark.pastEndOfWindow())
                    .withAllowedLateness(Duration.ZERO).discardingFiredPanes())
            .apply(ComputeTopCompletions.top(10, options.getRecursive()));

    toWrite.apply(ParDo.named("FormatForPerTaskFile").of(new FormatForPerTaskLocalFile()))
            .apply(TextIO.Write.to("./outputAutoComplete.txt"));

    p.run();
}

From source file:com.dataartisans.flink.dataflow.examples.streaming.JoinExamples.java

License:Apache License

public static void main(String[] args) throws Exception {
    Options options = PipelineOptionsFactory.fromArgs(args).withValidation().as(Options.class);
    options.setStreaming(true);//from   ww  w . j a v a  2 s  . c om
    options.setCheckpointingInterval(1000L);
    options.setNumberOfExecutionRetries(5);
    options.setExecutionRetryDelay(3000L);
    options.setRunner(FlinkPipelineRunner.class);

    PTransform<? super PBegin, PCollection<String>> readSourceA = Read
            .from(new UnboundedSocketSource<>("localhost", 9999, '\n', 3)).named("FirstStream");
    PTransform<? super PBegin, PCollection<String>> readSourceB = Read
            .from(new UnboundedSocketSource<>("localhost", 9998, '\n', 3)).named("SecondStream");

    WindowFn<Object, ?> windowFn = FixedWindows.of(Duration.standardSeconds(options.getWindowSize()));

    Pipeline p = Pipeline.create(options);

    // the following two 'applys' create multiple inputs to our pipeline, one for each
    // of our two input sources.
    PCollection<String> streamA = p.apply(readSourceA)
            .apply(Window.<String>into(windowFn).triggering(AfterWatermark.pastEndOfWindow())
                    .withAllowedLateness(Duration.ZERO).discardingFiredPanes());
    PCollection<String> streamB = p.apply(readSourceB)
            .apply(Window.<String>into(windowFn).triggering(AfterWatermark.pastEndOfWindow())
                    .withAllowedLateness(Duration.ZERO).discardingFiredPanes());

    PCollection<String> formattedResults = joinEvents(streamA, streamB);
    formattedResults.apply(TextIO.Write.to("./outputJoin.txt"));
    p.run();
}