Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change the persistence activity to use the new persistence layer #14205

Merged
merged 6 commits into from
Jun 28, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import io.airbyte.config.persistence.ConfigPersistence;
import io.airbyte.config.persistence.ConfigRepository;
import io.airbyte.config.persistence.DatabaseConfigPersistence;
import io.airbyte.config.persistence.StatePersistence;
import io.airbyte.config.persistence.StreamResetPersistence;
import io.airbyte.config.persistence.split_secrets.JsonSecretsProcessor;
import io.airbyte.config.persistence.split_secrets.SecretPersistence;
Expand Down Expand Up @@ -139,6 +140,7 @@ public class WorkerApp {
private final JobTracker jobTracker;
private final JobErrorReporter jobErrorReporter;
private final StreamResetPersistence streamResetPersistence;
private final StatePersistence statePersistence;

public void start() {
final Map<String, String> mdc = MDC.getCopyOfContextMap();
Expand Down Expand Up @@ -224,7 +226,7 @@ private void registerSync(final WorkerFactory factory) {
defaultWorkerConfigs,
defaultProcessFactory);

final PersistStateActivityImpl persistStateActivity = new PersistStateActivityImpl(workspaceRoot, configRepository);
final PersistStateActivityImpl persistStateActivity = new PersistStateActivityImpl(statePersistence);

final Worker syncWorker = factory.newWorker(TemporalJobType.SYNC.name(), getWorkerOptions(maxWorkers.getMaxSyncWorkers()));
syncWorker.registerWorkflowImplementationTypes(SyncWorkflowImpl.class);
Expand Down Expand Up @@ -445,6 +447,8 @@ private static void launchWorkerApp(final Configs configs, final DSLContext conf
new JobErrorReporter(configRepository, configs.getDeploymentMode(), configs.getAirbyteVersionOrWarning(), jobErrorReportingClient);

final StreamResetPersistence streamResetPersistence = new StreamResetPersistence(configDatabase);

final StatePersistence statePersistence = new StatePersistence(configDatabase);
new WorkerApp(
workspaceRoot,
defaultProcessFactory,
Expand Down Expand Up @@ -473,7 +477,8 @@ private static void launchWorkerApp(final Configs configs, final DSLContext conf
jobNotifier,
jobTracker,
jobErrorReporter,
streamResetPersistence).start();
streamResetPersistence,
statePersistence).start();
}

public static void main(final String[] args) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,30 +6,30 @@

import io.airbyte.config.StandardSyncOutput;
import io.airbyte.config.State;
import io.airbyte.config.persistence.ConfigRepository;
import io.airbyte.config.StateWrapper;
import io.airbyte.config.helpers.StateMessageHelper;
import io.airbyte.config.persistence.StatePersistence;
import java.io.IOException;
import java.nio.file.Path;
import java.util.Optional;
import java.util.UUID;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class PersistStateActivityImpl implements PersistStateActivity {

private static final Logger LOGGER = LoggerFactory.getLogger(PersistStateActivityImpl.class);
private final Path workspaceRoot;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changes that could have been avoided but it is a good occasion to do a cleanup

private final ConfigRepository configRepository;
private final StatePersistence statePersistence;

public PersistStateActivityImpl(final Path workspaceRoot, final ConfigRepository configRepository) {
this.workspaceRoot = workspaceRoot;
this.configRepository = configRepository;
public PersistStateActivityImpl(final StatePersistence statePersistence) {
this.statePersistence = statePersistence;
}

@Override
public boolean persist(final UUID connectionId, final StandardSyncOutput syncOutput) {
final State state = syncOutput.getState();
if (state != null) {
try {
configRepository.updateConnectionState(connectionId, state);
final Optional<StateWrapper> maybeStateWrapper = StateMessageHelper.getTypedState(state.getState());
if (maybeStateWrapper.isPresent()) {
statePersistence.updateOrCreateState(connectionId, maybeStateWrapper.get());
}
} catch (final IOException e) {
throw new RuntimeException(e);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package io.airbyte.workers.temporal.sync;

import com.fasterxml.jackson.databind.JsonNode;
import io.airbyte.commons.json.Jsons;
import io.airbyte.config.StandardSyncOutput;
import io.airbyte.config.State;
import io.airbyte.config.StateWrapper;
import io.airbyte.config.persistence.StatePersistence;
import java.io.IOException;
import java.util.UUID;
import org.elasticsearch.common.collect.Map;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.junit.jupiter.MockitoExtension;

@ExtendWith(MockitoExtension.class)
public class PersistStateActivityTest {

private final static UUID CONNECTION_ID = UUID.randomUUID();

@Mock
StatePersistence statePersistence;

@InjectMocks
PersistStateActivityImpl persistStateActivity;

@Test
public void testPersistEmpty() {
persistStateActivity.persist(CONNECTION_ID, new StandardSyncOutput());

Mockito.verifyNoInteractions(statePersistence);
}

@Test
public void testPersist() throws IOException {
final JsonNode jsonState = Jsons.jsonNode(Map.ofEntries(
Map.entry("some", "state")
));

final State state = new State().withState(jsonState);

persistStateActivity.persist(CONNECTION_ID, new StandardSyncOutput().withState(state));

// The ser/der of the state into a state wrapper is tested in StateMessageHelperTest
Mockito.verify(statePersistence).updateOrCreateState(Mockito.eq(CONNECTION_ID), Mockito.any(StateWrapper.class));
}
}