pulumi/tests/integration/dependency_steps/step1/resource.ts

70 lines
2.2 KiB
TypeScript
Raw Permalink Normal View History

Lift snapshot management out of the engine and serialize writes to snapshot (#1069) * Lift snapshot management out of the engine This PR is a prerequisite for parallelism by addressing a major problem that the engine has to deal with when performing parallel resource construction: parallel mutation of the global snapshot. This PR adds a `SnapshotManager` type that is responsible for maintaining and persisting the current resource snapshot. It serializes all reads and writes to the global snapshot and persists the snapshot to persistent storage upon every write. As a side-effect of this, the core engine no longer needs to know about snapshot management at all; all snapshot operations can be handled as callbacks on deployment events. This will greatly simplify the parallelization of the core engine. Worth noting is that the core engine will still need to be able to read the current snapshot, since it is interested in the dependency graphs contained within. The full implications of that are out of scope of this PR. Remove dead code, Steps no longer need a reference to the plan iterator that created them Fixing various issues that arise when bringing up pulumi-aws Line length broke the build Code review: remove dead field, fix yaml name error Rebase against master, provide implementation of StackPersister for cloud backend Code review feedback: comments on MutationStatus, style in snapshot.go Code review feedback: move SnapshotManager to pkg/backend, change engine to use an interface SnapshotManager Code review feedback: use a channel for synchronization Add a comment and a new test * Maintain two checkpoints, an immutable base and a mutable delta, and periodically merge the two to produce snapshots * Add a lot of tests - covers all of the non-error paths of BeginMutation and End * Fix a test resource provider * Add a few tests, fix a few issues * Rebase against master, fixed merge
2018-04-12 16:55:34 +00:00
// Copyright 2016-2018, Pulumi Corporation. All rights reserved.
import * as pulumi from "@pulumi/pulumi";
let currentID = 0;
export class Provider implements pulumi.dynamic.ResourceProvider {
public static readonly instance = new Provider();
private inject: Error | undefined;
public async diff(id: pulumi.ID, olds: any, news: any) {
let replaces: string[] = [];
let deleteBeforeReplace: boolean = false;
if ((olds as ResourceProps).replace !== (news as ResourceProps).replace) {
replaces.push("replace");
}
if ((olds as ResourceProps).replaceDBR !== (news as ResourceProps).replaceDBR) {
replaces.push("replaceDBR");
deleteBeforeReplace = true;
}
return {
replaces: replaces,
deleteBeforeReplace: deleteBeforeReplace,
};
}
public async create(inputs: any) {
if (this.inject) {
throw this.inject;
}
return {
id: (currentID++).toString(),
outs: undefined,
};
}
public async update(id: pulumi.ID, olds: any, news: any) {
if (this.inject) {
throw this.inject;
}
return {};
}
public async delete(id: pulumi.ID, props: any) {
if (this.inject) {
throw this.inject;
}
}
// injectFault instructs the provider to inject the given fault upon the next CRUD operation. Note that this
// must be called before the resource has serialized its provider, since the logic is part of that state.
public injectFault(error: Error | undefined): void {
this.inject = error;
}
}
export class Resource extends pulumi.dynamic.Resource {
constructor(name: string, props: ResourceProps, opts?: pulumi.ResourceOptions) {
super(Provider.instance, name, props, opts);
}
}
export interface ResourceProps {
state?: any; // arbitrary state bag that can be updated without replacing.
replace?: any; // arbitrary state bag that requires replacement when updating.
replaceDBR?: any; // arbitrary state bag that requires replacement (with delete-before-replace=true).
resource?: pulumi.Resource; // to force a dependency on a resource.
}