use of io.cdap.cdap.internal.app.store.AppMetadataStore in project cdap by caskdata.
the class PreviewTMSLogSubscriber method storeMessageId.
@Override
protected void storeMessageId(StructuredTableContext context, String messageId) throws Exception {
AppMetadataStore appMetadataStore = AppMetadataStore.create(context);
appMetadataStore.persistSubscriberState(getTopicId().getTopic(), CONSUMER_NAME, messageId);
}
use of io.cdap.cdap.internal.app.store.AppMetadataStore in project cdap by caskdata.
the class MetadataSubscriberService method preProcess.
@Override
protected void preProcess() {
if (didBackfill) {
return;
}
if (backfillAttempts > 10) {
LOG.info("Skipping attempt to back-fill plugin metadata after 10 failures.");
return;
}
// Backfill plugin metadata
backfillAttempts++;
LOG.info("Starting back-fill process(attempt {}) for plugin metadata", backfillAttempts);
boolean updateFailed = false;
NamespaceStore namespaceStore = new DefaultNamespaceStore(this.transactionRunner);
List<String> namespaces = namespaceStore.list().stream().map(NamespaceMeta::getName).collect(Collectors.toList());
LOG.debug("Back-filling plugin metadata for {} namespaces", namespaces.size());
for (String namespace : namespaces) {
List<ApplicationMeta> apps = TransactionRunners.run(this.transactionRunner, context -> {
AppMetadataStore appMetadataStore = AppMetadataStore.create(context);
return appMetadataStore.getAllApplications(namespace);
});
LOG.debug("Back-filling plugin metadata for namespace '{}' with {} applications", namespace, apps.size());
try {
this.getPluginCounts(namespace, apps);
} catch (IOException e) {
updateFailed = true;
LOG.warn("Failed to write plugin metadata updates for namespace '{}': {}", namespace, e);
}
}
if (!updateFailed) {
LOG.info("Successfully back-filled plugin metadata for {} namespaces.", namespaces.size());
didBackfill = true;
TransactionRunners.run(transactionRunner, (TxRunnable) context -> AppMetadataStore.create(context).persistSubscriberState(getTopicId().getTopic(), BACKFILL_SUBSCRIBER_NAME, "true"));
}
}
use of io.cdap.cdap.internal.app.store.AppMetadataStore in project cdap by caskdata.
the class TetheringAgentService method initializeMessageIds.
private void initializeMessageIds() {
List<String> peers;
try {
peers = store.getPeers().stream().map(PeerInfo::getName).collect(Collectors.toList());
} catch (IOException e) {
LOG.warn("Failed to get peer information", e);
return;
}
TransactionRunners.run(transactionRunner, context -> {
AppMetadataStore appMetadataStore = AppMetadataStore.create(context);
for (String peer : peers) {
String messageId = appMetadataStore.retrieveSubscriberState(peer, SUBSCRIBER);
lastMessageIds.put(peer, messageId);
}
});
}
use of io.cdap.cdap.internal.app.store.AppMetadataStore in project cdap by caskdata.
the class TetheringAgentService method runTask.
@Override
protected long runTask() {
List<PeerInfo> peers;
try {
peers = store.getPeers().stream().filter(p -> p.getTetheringStatus() != TetheringStatus.REJECTED).collect(Collectors.toList());
} catch (IOException e) {
LOG.warn("Failed to get peer information", e);
return connectionInterval;
}
for (PeerInfo peer : peers) {
try {
Preconditions.checkArgument(peer.getEndpoint() != null, "Peer %s doesn't have an endpoint", peer.getName());
String uri = CONNECT_CONTROL_CHANNEL + instanceName;
String lastMessageId = lastMessageIds.get(peer);
if (lastMessageId != null) {
uri = uri + "?messageId=" + URLEncoder.encode(lastMessageId, "UTF-8");
}
HttpResponse resp = TetheringUtils.sendHttpRequest(remoteAuthenticator, HttpMethod.GET, new URI(peer.getEndpoint()).resolve(uri));
switch(resp.getResponseCode()) {
case HttpURLConnection.HTTP_OK:
handleResponse(resp, peer);
break;
case HttpURLConnection.HTTP_NOT_FOUND:
handleNotFound(peer);
break;
case HttpURLConnection.HTTP_FORBIDDEN:
handleForbidden(peer);
break;
default:
LOG.error("Peer {} returned unexpected error code {} body {}", peer.getName(), resp.getResponseCode(), resp.getResponseBodyAsString(StandardCharsets.UTF_8));
}
} catch (Exception e) {
LOG.debug("Failed to create control channel to {}", peer, e);
}
}
// Update last message ids in the store for all peers
TransactionRunners.run(transactionRunner, context -> {
AppMetadataStore appMetadataStore = AppMetadataStore.create(context);
for (Map.Entry<String, String> entry : lastMessageIds.entrySet()) {
appMetadataStore.persistSubscriberState(entry.getKey(), SUBSCRIBER, entry.getValue());
}
});
return connectionInterval;
}
use of io.cdap.cdap.internal.app.store.AppMetadataStore in project cdap by caskdata.
the class DirectRuntimeRequestValidatorTest method testValidProgramInStoppingState.
@Test
public void testValidProgramInStoppingState() throws BadRequestException {
ProgramRunId programRunId = NamespaceId.DEFAULT.app("app").spark("spark").run(RunIds.generate());
// Insert the run
TransactionRunners.run(txRunner, context -> {
AppMetadataStore store = AppMetadataStore.create(context);
store.recordProgramProvisioning(programRunId, Collections.emptyMap(), Collections.singletonMap(SystemArguments.PROFILE_NAME, "system:default"), createSourceId(1), ARTIFACT_ID);
store.recordProgramProvisioned(programRunId, 1, createSourceId(2));
store.recordProgramStart(programRunId, null, Collections.emptyMap(), createSourceId(3));
store.recordProgramRunning(programRunId, System.currentTimeMillis(), null, createSourceId(3));
store.recordProgramStopping(programRunId, createSourceId(3), System.currentTimeMillis(), System.currentTimeMillis() + 1000);
});
// Validation should pass
RuntimeRequestValidator validator = new DirectRuntimeRequestValidator(cConf, txRunner, new MockProgramRunRecordFetcher(), accessEnforcer, authenticationContext);
ProgramRunInfo programRunInfo = validator.getProgramRunStatus(programRunId, new DefaultHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.GET, "/"));
// After recording the program start in AppMetaDataStore the expected state is Stopping
Assert.assertEquals(ProgramRunStatus.STOPPING, programRunInfo.getProgramRunStatus());
Assert.assertNotNull("Payload isn't null when program status is Stopping", programRunInfo.getPayload());
}
Aggregations