use of com.google.common.util.concurrent.Futures in project metacat by Netflix.
the class HiveConnectorFastPartitionService method getpartitions.
private List<PartitionInfo> getpartitions(@Nonnull @NonNull final String databaseName, @Nonnull @NonNull final String tableName, @Nullable final List<String> partitionIds, final String filterExpression, final Sort sort, final Pageable pageable, final boolean includePartitionDetails) {
final FilterPartition filter = new FilterPartition();
// batch exists
final boolean isBatched = !Strings.isNullOrEmpty(filterExpression) && filterExpression.contains(FIELD_BATCHID);
final boolean hasDateCreated = !Strings.isNullOrEmpty(filterExpression) && filterExpression.contains(FIELD_DATE_CREATED);
// Handler for reading the result set
final ResultSetHandler<List<PartitionDetail>> handler = rs -> {
final List<PartitionDetail> result = Lists.newArrayList();
while (rs.next()) {
final String name = rs.getString("name");
final String uri = rs.getString("uri");
final long createdDate = rs.getLong(FIELD_DATE_CREATED);
Map<String, String> values = null;
if (hasDateCreated) {
values = Maps.newHashMap();
values.put(FIELD_DATE_CREATED, createdDate + "");
}
if (Strings.isNullOrEmpty(filterExpression) || filter.evaluatePartitionExpression(filterExpression, name, uri, isBatched, values)) {
final Long id = rs.getLong("id");
final Long sdId = rs.getLong("sd_id");
final Long serdeId = rs.getLong("serde_id");
final String inputFormat = rs.getString("input_format");
final String outputFormat = rs.getString("output_format");
final String serializationLib = rs.getString("slib");
final StorageInfo storageInfo = new StorageInfo();
storageInfo.setUri(uri);
storageInfo.setInputFormat(inputFormat);
storageInfo.setOutputFormat(outputFormat);
storageInfo.setSerializationLib(serializationLib);
final AuditInfo auditInfo = new AuditInfo();
auditInfo.setCreatedDate(Date.from(Instant.ofEpochSecond(createdDate)));
auditInfo.setLastModifiedDate(Date.from(Instant.ofEpochSecond(createdDate)));
result.add(new PartitionDetail(id, sdId, serdeId, PartitionInfo.builder().name(QualifiedName.ofPartition(catalogName, databaseName, tableName, name)).auditInfo(auditInfo).serde(storageInfo).build()));
}
}
return result;
};
final List<PartitionInfo> partitionInfos = new ArrayList<>();
final List<PartitionDetail> partitions = getHandlerResults(databaseName, tableName, filterExpression, partitionIds, SQL_GET_PARTITIONS, handler, sort, pageable);
if (includePartitionDetails && !partitions.isEmpty()) {
final List<Long> partIds = Lists.newArrayListWithCapacity(partitions.size());
final List<Long> sdIds = Lists.newArrayListWithCapacity(partitions.size());
final List<Long> serdeIds = Lists.newArrayListWithCapacity(partitions.size());
for (PartitionDetail partitionDetail : partitions) {
partIds.add(partitionDetail.getId());
sdIds.add(partitionDetail.getSdId());
serdeIds.add(partitionDetail.getSerdeId());
}
final List<ListenableFuture<Void>> futures = Lists.newArrayList();
final Map<Long, Map<String, String>> partitionParams = Maps.newHashMap();
futures.add(threadServiceManager.getExecutor().submit(() -> populateParameters(partIds, SQL_GET_PARTITION_PARAMS, "part_id", partitionParams)));
final Map<Long, Map<String, String>> sdParams = Maps.newHashMap();
if (!sdIds.isEmpty()) {
futures.add(threadServiceManager.getExecutor().submit(() -> populateParameters(sdIds, SQL_GET_SD_PARAMS, "sd_id", sdParams)));
}
final Map<Long, Map<String, String>> serdeParams = Maps.newHashMap();
if (!serdeIds.isEmpty()) {
futures.add(threadServiceManager.getExecutor().submit(() -> populateParameters(serdeIds, SQL_GET_SERDE_PARAMS, "serde_id", serdeParams)));
}
try {
Futures.transform(Futures.successfulAsList(futures), Functions.constant(null)).get(1, TimeUnit.HOURS);
} catch (Exception e) {
Throwables.propagate(e);
}
for (PartitionDetail partitionDetail : partitions) {
partitionDetail.getPartitionInfo().setMetadata(partitionParams.get(partitionDetail.getId()));
partitionDetail.getPartitionInfo().getSerde().setParameters(sdParams.get(partitionDetail.getSdId()));
partitionDetail.getPartitionInfo().getSerde().setSerdeInfoParameters(serdeParams.get(partitionDetail.getSerdeId()));
}
}
for (PartitionDetail partitionDetail : partitions) {
partitionInfos.add(partitionDetail.getPartitionInfo());
}
return partitionInfos;
}
use of com.google.common.util.concurrent.Futures in project buck by facebook.
the class ResourcePoolTest method doesNotCreateMoreThanMaxResources.
@Test
public void doesNotCreateMoreThanMaxResources() throws Exception {
try (Fixture f = new Fixture()) {
CountDownLatch waitTillAllThreadsAreBusy = new CountDownLatch(f.getMaxResources());
CountDownLatch unblockAllThreads = new CountDownLatch(1);
List<ListenableFuture<?>> futures = new ArrayList<>();
for (int i = 0; i < f.getMaxResources() * 10; i++) {
futures.add(f.getPool().scheduleOperationWithResource(r -> {
waitTillAllThreadsAreBusy.countDown();
unblockAllThreads.await();
return r;
}, f.getExecutorService()));
}
waitTillAllThreadsAreBusy.await();
unblockAllThreads.countDown();
Futures.allAsList(futures).get();
assertThat(f.getCreatedResources().get(), equalTo(2));
}
}
use of com.google.common.util.concurrent.Futures in project thingsboard by thingsboard.
the class DeviceServiceImpl method findDevicesByQuery.
@Override
public ListenableFuture<List<Device>> findDevicesByQuery(DeviceSearchQuery query) {
ListenableFuture<List<EntityRelation>> relations = relationService.findByQuery(query.toEntitySearchQuery());
ListenableFuture<List<Device>> devices = Futures.transform(relations, (AsyncFunction<List<EntityRelation>, List<Device>>) relations1 -> {
EntitySearchDirection direction = query.toEntitySearchQuery().getParameters().getDirection();
List<ListenableFuture<Device>> futures = new ArrayList<>();
for (EntityRelation relation : relations1) {
EntityId entityId = direction == EntitySearchDirection.FROM ? relation.getTo() : relation.getFrom();
if (entityId.getEntityType() == EntityType.DEVICE) {
futures.add(findDeviceByIdAsync(new DeviceId(entityId.getId())));
}
}
return Futures.successfulAsList(futures);
});
devices = Futures.transform(devices, new Function<List<Device>, List<Device>>() {
@Nullable
@Override
public List<Device> apply(@Nullable List<Device> deviceList) {
return deviceList == null ? Collections.emptyList() : deviceList.stream().filter(device -> query.getDeviceTypes().contains(device.getType())).collect(Collectors.toList());
}
});
return devices;
}
use of com.google.common.util.concurrent.Futures in project netvirt by opendaylight.
the class InterfaceStateChangeListener method update.
// TODO Clean up the exception handling
@SuppressWarnings("checkstyle:IllegalCatch")
@Override
protected void update(InstanceIdentifier<Interface> identifier, Interface original, Interface update) {
final String ifName = update.getName();
try {
OperStatus originalOperStatus = original.getOperStatus();
OperStatus updateOperStatus = update.getOperStatus();
if (originalOperStatus.equals(Interface.OperStatus.Unknown) || updateOperStatus.equals(Interface.OperStatus.Unknown)) {
LOG.debug("Interface {} state change is from/to null/UNKNOWN. Ignoring the update event.", ifName);
return;
}
if (update.getIfIndex() == null) {
return;
}
if (L2vlan.class.equals(update.getType())) {
LOG.info("VPN Interface update event - intfName {} from InterfaceStateChangeListener", update.getName());
jobCoordinator.enqueueJob("VPNINTERFACE-" + ifName, () -> {
List<ListenableFuture<Void>> futures = new ArrayList<>(3);
futures.add(txRunner.callWithNewWriteOnlyTransactionAndSubmit(writeOperTxn -> {
futures.add(txRunner.callWithNewWriteOnlyTransactionAndSubmit(writeConfigTxn -> {
futures.add(txRunner.callWithNewWriteOnlyTransactionAndSubmit(writeInvTxn -> {
final VpnInterface vpnIf = VpnUtil.getConfiguredVpnInterface(dataBroker, ifName);
if (vpnIf != null) {
final int ifIndex = update.getIfIndex();
BigInteger dpnId = BigInteger.ZERO;
try {
dpnId = InterfaceUtils.getDpIdFromInterface(update);
} catch (Exception e) {
LOG.error("remove: Unable to retrieve dpnId for interface {}", ifName, e);
return;
}
if (update.getOperStatus().equals(Interface.OperStatus.Up)) {
for (VpnInstanceNames vpnInterfaceVpnInstance : vpnIf.getVpnInstanceNames()) {
String vpnName = vpnInterfaceVpnInstance.getVpnName();
String primaryRd = VpnUtil.getPrimaryRd(dataBroker, vpnName);
if (!vpnInterfaceManager.isVpnInstanceReady(vpnName)) {
LOG.error("VPN Interface update event - intfName {} onto vpnName {} " + "running oper-driven UP, VpnInstance not ready," + " holding on", vpnIf.getName(), vpnName);
} else if (VpnUtil.isVpnPendingDelete(dataBroker, primaryRd)) {
LOG.error("update: Ignoring UP event for vpnInterface {}, as " + "vpnInstance {} with primaryRd {} is already marked for" + " deletion", vpnIf.getName(), vpnName, primaryRd);
} else {
vpnInterfaceManager.processVpnInterfaceUp(dpnId, vpnIf, primaryRd, ifIndex, true, writeConfigTxn, writeOperTxn, writeInvTxn, update, vpnName);
}
}
} else if (update.getOperStatus().equals(Interface.OperStatus.Down)) {
for (VpnInstanceNames vpnInterfaceVpnInstance : vpnIf.getVpnInstanceNames()) {
String vpnName = vpnInterfaceVpnInstance.getVpnName();
LOG.info("VPN Interface update event - intfName {} onto vpnName {}" + " running oper-driven DOWN", vpnIf.getName(), vpnName);
Optional<VpnInterfaceOpDataEntry> optVpnInterface = VpnUtil.getVpnInterfaceOpDataEntry(dataBroker, vpnIf.getName(), vpnName);
if (optVpnInterface.isPresent()) {
VpnInterfaceOpDataEntry vpnOpInterface = optVpnInterface.get();
vpnInterfaceManager.processVpnInterfaceDown(dpnId, vpnIf.getName(), ifIndex, update.getPhysAddress().getValue(), vpnOpInterface, true, writeConfigTxn, writeOperTxn, writeInvTxn);
} else {
LOG.error("InterfaceStateChangeListener Update DOWN - vpnInterface {}" + " not available, ignoring event", vpnIf.getName());
continue;
}
}
}
} else {
LOG.debug("Interface {} is not a vpninterface, ignoring.", ifName);
}
}));
}));
}));
return futures;
});
}
} catch (Exception e) {
LOG.error("Exception observed in handling updation of VPN Interface {}. ", update.getName(), e);
}
}
use of com.google.common.util.concurrent.Futures in project netvirt by opendaylight.
the class InterfaceStateChangeListener method remove.
@Override
// TODO Clean up the exception handling
@SuppressWarnings("checkstyle:IllegalCatch")
protected void remove(InstanceIdentifier<Interface> identifier, Interface intrf) {
final String ifName = intrf.getName();
BigInteger dpId = BigInteger.ZERO;
try {
if (L2vlan.class.equals(intrf.getType())) {
LOG.info("VPN Interface remove event - intfName {} from InterfaceStateChangeListener", intrf.getName());
try {
dpId = InterfaceUtils.getDpIdFromInterface(intrf);
} catch (Exception e) {
LOG.error("Unable to retrieve dpnId from interface operational data store for interface" + " {}. Fetching from vpn interface op data store. ", ifName, e);
}
final BigInteger inputDpId = dpId;
jobCoordinator.enqueueJob("VPNINTERFACE-" + ifName, () -> {
List<ListenableFuture<Void>> futures = new ArrayList<>(3);
ListenableFuture<Void> configFuture = txRunner.callWithNewWriteOnlyTransactionAndSubmit(writeConfigTxn -> {
futures.add(txRunner.callWithNewWriteOnlyTransactionAndSubmit(writeOperTxn -> {
futures.add(txRunner.callWithNewWriteOnlyTransactionAndSubmit(writeInvTxn -> {
VpnInterface cfgVpnInterface = VpnUtil.getConfiguredVpnInterface(dataBroker, ifName);
if (cfgVpnInterface == null) {
LOG.debug("Interface {} is not a vpninterface, ignoring.", ifName);
return;
}
for (VpnInstanceNames vpnInterfaceVpnInstance : cfgVpnInterface.getVpnInstanceNames()) {
String vpnName = vpnInterfaceVpnInstance.getVpnName();
Optional<VpnInterfaceOpDataEntry> optVpnInterface = VpnUtil.getVpnInterfaceOpDataEntry(dataBroker, ifName, vpnName);
if (!optVpnInterface.isPresent()) {
LOG.debug("Interface {} vpn {} is not a vpninterface, or deletion" + " triggered by northbound agent. ignoring.", ifName, vpnName);
continue;
}
final VpnInterfaceOpDataEntry vpnInterface = optVpnInterface.get();
String gwMac = intrf.getPhysAddress() != null ? intrf.getPhysAddress().getValue() : vpnInterface.getGatewayMacAddress();
BigInteger dpnId = inputDpId;
if (dpnId == null || dpnId.equals(BigInteger.ZERO)) {
dpnId = vpnInterface.getDpnId();
}
final int ifIndex = intrf.getIfIndex();
LOG.info("VPN Interface remove event - intfName {} onto vpnName {}" + " running oper-driver", vpnInterface.getName(), vpnName);
vpnInterfaceManager.processVpnInterfaceDown(dpnId, ifName, ifIndex, gwMac, vpnInterface, false, writeConfigTxn, writeOperTxn, writeInvTxn);
}
}));
}));
});
futures.add(configFuture);
Futures.addCallback(configFuture, new PostVpnInterfaceThreadWorker(intrf.getName(), false, "Operational"));
return futures;
}, DJC_MAX_RETRIES);
}
} catch (Exception e) {
LOG.error("Exception observed in handling deletion of VPN Interface {}. ", ifName, e);
}
}
Aggregations