Skip to content

Commit a230eb0

Browse files
author
Kerkesni
committed
Pass instanceId to the mongodb metadata backend
instanceId will be used when generating versionIds. This will ensure uniqueness of the versionID across all Cloudserver pods in the cluster. The instanceId is a combination of the short hash contained in the name of the pods, which is unique across all pods within the same deployment, and a prefix for differenciating the different types of deployments (internal vs external). see. https://scality.atlassian.net/wiki/spaces/OS/pages/3225583692/VersionID+Collisions+in+Zenko Issue: CLDSRV-665
1 parent 189d2d6 commit a230eb0

File tree

3 files changed

+62
-0
lines changed

3 files changed

+62
-0
lines changed

lib/Config.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -910,6 +910,29 @@ class Config extends EventEmitter {
910910
this.replicationGroupId = 'RG001';
911911
}
912912

913+
if (process.env.HOSTNAME) {
914+
// If running in Kubernetes, the pod name is set in the HOSTNAME env var
915+
// and is of the form: cloudserver-7869f99955-nr984
916+
// where:
917+
// - cloudserver is the name of the pod
918+
// - 7869f99955 is a long hash that guarantees uniqueness within the namespace
919+
// - nr984 is a short hash that is unique only within the current ReplicaSet/Deployment
920+
// We use the last part of the pod name as the instanceId, prefixed with 'i' for internal
921+
// or 'e' for external cloudserver.
922+
// This allows having a unique identifier that will be used when generating versionIds
923+
const instanceType = process.env.HOSTNAME?.includes('internal') ? 'i' : 'e';
924+
const parts = process.env.HOSTNAME.split('-');
925+
if (parts.length > 2) {
926+
const shortId = parts[parts.length - 1];
927+
this.instanceId = instanceType + shortId;
928+
} else {
929+
// fallback: generate a random string if format is unexpected
930+
this.instanceId = uuidv4().replace(/-/g, '').slice(0, 6);
931+
}
932+
} else {
933+
this.instanceId = uuidv4().replace(/-/g, '').slice(0, 6);
934+
}
935+
913936
this.replicationEndpoints = [];
914937
if (config.replicationEndpoints) {
915938
const { replicationEndpoints } = config;

lib/metadata/wrapper.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ if (clientName === 'mem') {
3030
params = {
3131
mongodb: config.mongodb,
3232
replicationGroupId: config.replicationGroupId,
33+
instanceId: config.instanceId,
3334
config,
3435
};
3536
} else if (clientName === 'cdmi') {

tests/unit/Config.js

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -810,4 +810,42 @@ describe('Config', () => {
810810
assert.deepEqual(config.internalListenOn, [{ ip: '127.0.0.1', port: 9000 }]);
811811
});
812812
});
813+
814+
describe('instanceId', () => {
815+
let previousHostname;
816+
817+
beforeEach(() => {
818+
previousHostname = process.env.HOSTNAME;
819+
});
820+
821+
afterEach(() => {
822+
if (previousHostname) {
823+
process.env.HOSTNAME = previousHostname;
824+
} else {
825+
delete process.env.HOSTNAME;
826+
}
827+
});
828+
829+
it('should generate a new instanceId for external cloudserver', () => {
830+
process.env.HOSTNAME = 'connector-cloudserver-69958b4697-bs5pn';
831+
const config = new ConfigObject();
832+
assert.strictEqual(config.instanceId, 'ebs5pn');
833+
});
834+
835+
it('should generate a new instanceId for internal cloudserver', () => {
836+
process.env.HOSTNAME = 'internal-cloudserver-54dc76b796-48m2x';
837+
const config = new ConfigObject();
838+
assert.strictEqual(config.instanceId, 'i48m2x');
839+
});
840+
it('should generate a random instanceId if HOSTNAME has an unexpected format', () => {
841+
process.env.HOSTNAME = 'cldsrv1';
842+
const config = new ConfigObject();
843+
assert.strictEqual(config.instanceId.length, 6);
844+
});
845+
it('should generate a random instanceId if HOSTNAME is not set', () => {
846+
delete process.env.HOSTNAME;
847+
const config = new ConfigObject();
848+
assert.strictEqual(config.instanceId.length, 6);
849+
});
850+
});
813851
});

0 commit comments

Comments
 (0)