@Test
  public void testModifySnapshotBeforeCommit() throws Exception {
    expect(snapshotStore.createSnapshot()).andReturn(SNAPSHOT1);
    Snapshot modified = SNAPSHOT1.deepCopy().setTasks(ImmutableSet.of(TASK1));
    Capture<MutateWork<Object, Exception>> transaction = createCapture();
    expect(primaryStorage.write(capture(transaction))).andReturn(null);
    distributedStore.persist(modified);
    shutDownNow.execute();

    control.replay();

    clock.advance(INTERVAL);
    storageBackup.createSnapshot();
    String backup1 = storageBackup.createBackupName();
    recovery.stage(backup1);
    assertEquals(
        IScheduledTask.setFromBuilders(SNAPSHOT1.getTasks()), recovery.query(Query.unscoped()));
    recovery.deleteTasks(Query.taskScoped(Tasks.id(TASK2)));
    assertEquals(
        IScheduledTask.setFromBuilders(modified.getTasks()), recovery.query(Query.unscoped()));
    recovery.commit();
    transaction.getValue().apply(storeProvider);
  }
示例#2
0
    @Override
    public TemporaryStorage apply(Snapshot snapshot) {
      final Storage storage = DbUtil.createFlaggedStorage();
      FakeClock clock = new FakeClock();
      clock.setNowMillis(snapshot.getTimestamp());
      final SnapshotStore<Snapshot> snapshotStore = new SnapshotStoreImpl(clock, storage);
      snapshotStore.applySnapshot(snapshot);

      return new TemporaryStorage() {
        @Override
        public void deleteTasks(final Query.Builder query) {
          storage.write(
              new MutateWork.NoResult.Quiet() {
                @Override
                public void execute(MutableStoreProvider storeProvider) {
                  Set<String> ids =
                      FluentIterable.from(storeProvider.getTaskStore().fetchTasks(query))
                          .transform(Tasks::id)
                          .toSet();
                  storeProvider.getUnsafeTaskStore().deleteTasks(ids);
                }
              });
        }

        @Override
        public Iterable<IScheduledTask> fetchTasks(final Query.Builder query) {
          return storage.read(
              new Work.Quiet<Iterable<IScheduledTask>>() {
                @Override
                public Iterable<IScheduledTask> apply(StoreProvider storeProvider) {
                  return storeProvider.getTaskStore().fetchTasks(query);
                }
              });
        }

        @Override
        public Snapshot toSnapshot() {
          return snapshotStore.createSnapshot();
        }
      };
    }