Skip to content

Commit

Permalink
fix: Migrate to using new docPath for Firestore including projectID (#…
Browse files Browse the repository at this point in the history
…215)

fixes #213
  • Loading branch information
nielm committed Mar 12, 2024
1 parent 2eb433f commit 3d9771d
Show file tree
Hide file tree
Showing 2 changed files with 196 additions and 81 deletions.
55 changes: 44 additions & 11 deletions src/scaler/scaler-core/state.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@

const firestore = require('@google-cloud/firestore');
const {Spanner} = require('@google-cloud/spanner');
const {logger} = require('../../autoscaler-common/logger');
/**
* @typedef {import('../../autoscaler-common/types').AutoscalerSpanner
* } AutoscalerSpanner
Expand Down Expand Up @@ -179,12 +180,14 @@ class StateSpanner extends State {
keySet: {keys: [{values: [{stringValue: this.getSpannerId()}]}]},
};
const [rows] = await this.table.read(query);

let data;
if (rows.length == 0) {
this.data = await this.init();
data = await this.init();
} else {
this.data = rows[0].toJSON();
data = rows[0].toJSON();
}
return this.toMillis(this.data);
return this.toMillis(data);
}

/** @inheritdoc */
Expand Down Expand Up @@ -263,9 +266,8 @@ class StateFirestore extends State {
*/
get docRef() {
if (this._docRef == null) {
this.firestore = new firestore.Firestore({projectId: this.projectId});
this._docRef =
this.firestore.collection('spannerAutoscaler').doc(this.instanceId);
this._docRef = this.firestore
.doc(`spannerAutoscaler/state/${this.getSpannerId()}`);
}
return this._docRef;
}
Expand Down Expand Up @@ -299,18 +301,49 @@ class StateFirestore extends State {

/** @inheritdoc */
async get() {
const snapshot = await this.docRef.get(); // returns QueryDocumentSnapshot
let snapshot = await this.docRef.get(); // returns QueryDocumentSnapshot

if (!snapshot.exists) {
this.data = await this.init();
// It is possible that an old state doc exists in an old docref path...
snapshot = await this.checkAndReplaceOldDocRef();
}

let data;
if (!snapshot?.exists) {
data = await this.init();
} else {
this.data = snapshot.data();
data = snapshot.data();
}

return this.toMillis(this.data);
return this.toMillis(data);
}

/** @inheritdoc */
/**
* Due to [issue 213](https://github.com/cloudspannerecosystem/autoscaler/issues/213)
* the docRef had to be changed, so check for an old doc at the old docref
* If it exists, copy it to the new docref, delete it and return it.
*/
async checkAndReplaceOldDocRef() {
try {
const oldDocRef =
this.firestore.doc(`spannerAutoscaler/${this.instanceId}`);
const snapshot = await oldDocRef.get();
if (snapshot.exists) {
logger.info(`Migrating firestore doc path from spannerAutoscaler/${
this.instanceId} to spannerAutoscaler/state/${this.getSpannerId()}`);
await this.docRef.set(snapshot.data());
await oldDocRef.delete();
}
return snapshot;
} catch (e) {
logger.error(e, `Failed to migrate docpaths`);
}
return null;
}

/**
* Update scaling timestamp in storage
*/
async set() {
await this.get(); // make sure doc exists

Expand Down

0 comments on commit 3d9771d

Please sign in to comment.