Skip to content

Commit

Permalink
Fix code-move things around
Browse files Browse the repository at this point in the history
  • Loading branch information
henokgetachew committed Oct 22, 2024
1 parent 2331843 commit 0b8128f
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 16 deletions.
23 changes: 7 additions & 16 deletions src/move-node.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const utils = require('./utils');
const moveShard = require('./move-shard');

const replaceNodeInStandAloneCouch = async (toNode) => {
const replaceForSingleNode = async (toNode) => {
const removedNodes = [];

if (!toNode) {
Expand All @@ -17,10 +17,10 @@ const replaceNodeInStandAloneCouch = async (toNode) => {
const oldNodes = await moveShard.moveShard(shard, toNode);
removedNodes.push(...oldNodes);
}
return removedNodes;
return [...new Set(removedNodes)];
};

const replaceNodeInClusteredCouch = async (toNode, shardMapJson) => {
const replaceInCluster = async (toNode, shardMapJson) => {
const removedNodes = [];
if (!shardMapJson) {
throw new Error('Shard map JSON is required for multi-node migration');
Expand All @@ -39,26 +39,17 @@ const replaceNodeInClusteredCouch = async (toNode, shardMapJson) => {
if (!removedNodes.includes(oldNode)) {
removedNodes.push(oldNode);
}
return [...new Set(removedNodes)];
return [...new Set(removedNodes)];
};

const moveNode = async (nodeMap, shardMapJson) => {
if (typeof nodeMap === 'object') {
return await replaceInCluster(nodeMap, shardMapJson);
}

return await replaceForSingleNode(nodeMap);
};
let removedNodes = [];
if (typeof toNode === 'object') {
// Multi-node migration - replace node in clustered couch
removedNodes = await replaceNodeInClusteredCouch(toNode, shardMapJson);
} else {
// Single node migration - replace node in standalone couch
removedNodes = await replaceNodeInStandAloneCouch(toNode);
return await replaceInCluster(nodeMap, shardMapJson);
}

return [...new Set(removedNodes)];
// Single node migration - replace node in standalone couch
return await replaceForSingleNode(nodeMap);
};

const syncShards = async () => {
Expand Down
30 changes: 30 additions & 0 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,35 @@ const getShards = async () => {
}
};

/*
This function creates a mapping of shard ranges to nodes.
*/
const getShardMapping = async () => {
try {
const allDbs = await getDbs();
const shardMap = {};

for (const db of allDbs) {
const url = await getUrl(`${db}/_shards`);
const shardInfo = await request({ url });

for (const [shardRange, nodeList] of Object.entries(shardInfo.shards)) {
// In n=1 setup, there should be only one node per shard range.
// We will have to revisit this if we ever support n>1.
if (nodeList.length !== 1) {
console.warn(`Unexpected number of nodes for range ${shardRange}: ${nodeList.length}`);
}
shardMap[shardRange] = nodeList[0];
}
}

return JSON.stringify(shardMap);
} catch (err) {
console.error('Error getting shard mapping:', err);
throw new Error('Failed to get shard mapping');
}
};

const getNodes = async () => {
const membership = await getMembership();
return membership.all_nodes;
Expand Down Expand Up @@ -257,6 +286,7 @@ module.exports = {
deleteNode,
syncShards,
getShards,
getShardMapping,
getNodes,
getConfig,
getCouchUrl,
Expand Down

0 comments on commit 0b8128f

Please sign in to comment.