Skip to content

Commit

Permalink
pass current inventory instead of count to cloud-specific implementat…
Browse files Browse the repository at this point in the history
…ions
  • Loading branch information
aaronkvanmeerten committed Apr 18, 2024
1 parent 0040e99 commit d472ada
Show file tree
Hide file tree
Showing 7 changed files with 190 additions and 66 deletions.
5 changes: 3 additions & 2 deletions src/cloud_instance_manager.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { InstanceGroup } from './instance_group';
import { Context } from './context';
import { CloudRetryStrategy } from './cloud_manager';
import { InstanceState } from './instance_tracker';

export interface CloudInstance {
instanceId: string;
Expand All @@ -12,7 +13,7 @@ export interface CloudInstanceManager {
launchInstances(
ctx: Context,
group: InstanceGroup,
groupCurrentCount: number,
currentInventory: InstanceState[],
quantity: number,
): Promise<Array<string | boolean>>;

Expand All @@ -33,7 +34,7 @@ export abstract class AbstractCloudInstanceManager implements CloudInstanceManag
async launchInstances(
ctx: Context,
group: InstanceGroup,
groupCurrentCount: number,
currentInventory: InstanceState[],
quantity: number,
): Promise<Array<string | boolean>> {
ctx.logger.info(`[CloudInstanceManager] Launching a batch of ${quantity} instances in group ${group.name}`);
Expand Down
4 changes: 2 additions & 2 deletions src/cloud_manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ export default class CloudManager {
async scaleUp(
ctx: Context,
group: InstanceGroup,
groupCurrentCount: number,
currentInventory: InstanceState[],
quantity: number,
isScaleDownProtected: boolean,
): Promise<number> {
Expand All @@ -91,7 +91,7 @@ export default class CloudManager {
return 0;
}

const scaleUpResult = await instanceManager.launchInstances(ctx, group, groupCurrentCount, quantity);
const scaleUpResult = await instanceManager.launchInstances(ctx, group, currentInventory, quantity);

let scaleUpCount = 0;
await Promise.all(
Expand Down
3 changes: 2 additions & 1 deletion src/custom_instance_manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { execFile } from 'child_process';
import { InstanceGroup } from './instance_group';
import { Context } from './context';
import { AbstractCloudInstanceManager } from './cloud_instance_manager';
import { InstanceState } from './instance_tracker';

export interface CustomInstanceManagerOptions {
isDryRun: boolean;
Expand All @@ -27,7 +28,7 @@ export default class CustomInstanceManager extends AbstractCloudInstanceManager
async launchInstances(
ctx: Context,
group: InstanceGroup,
groupCurrentCount: number,
currentInventory: InstanceState[],
quantity: number,
): Promise<Array<string | boolean>> {
ctx.logger.info(`[custom] Launching a batch of ${quantity} instances in group ${group.name}`);
Expand Down
2 changes: 1 addition & 1 deletion src/instance_launcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ export default class InstanceLauncher {
const scaleUpCount = await this.cloudManager.scaleUp(
ctx,
group,
count,
currentInventory,
actualScaleUpQuantity,
scaleDownProtected,
);
Expand Down
3 changes: 2 additions & 1 deletion src/oracle_instance_manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { ResourceSearchClient } from 'oci-resourcesearch';
import * as resourceSearch from 'oci-resourcesearch';
import { CloudRetryStrategy } from './cloud_manager';
import { AbstractCloudInstanceManager, CloudInstanceManager, CloudInstance } from './cloud_instance_manager';
import { InstanceState } from './instance_tracker';

interface FaultDomainMap {
[key: string]: string[];
Expand Down Expand Up @@ -43,7 +44,7 @@ export default class OracleInstanceManager implements CloudInstanceManager {
async launchInstances(
ctx: Context,
group: InstanceGroup,
groupCurrentCount: number,
currentInventory: InstanceState[],
quantity: number,
): Promise<Array<string | boolean>> {
ctx.logger.info(`[oracle] Launching a batch of ${quantity} instances in group ${group.name}`);
Expand Down
56 changes: 36 additions & 20 deletions src/oracle_instance_pool_manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { Context } from './context';
import { CloudRetryStrategy } from './cloud_manager';
import { CloudInstanceManager, CloudInstance } from './cloud_instance_manager';
import { workrequests } from 'oci-sdk';
import { InstanceState } from './instance_tracker';

const maxTimeInSeconds = 60 * 60; // The duration for waiter configuration before failing. Currently set to 1 hour.
const maxDelayInSeconds = 30; // The max delay for the waiter configuration. Currently set to 30 seconds
Expand Down Expand Up @@ -65,11 +66,13 @@ export default class OracleInstancePoolManager implements CloudInstanceManager {
async launchInstances(
ctx: Context,
group: InstanceGroup,
groupCurrentCount: number,
currentInventory: InstanceState[],
quantity: number,
): Promise<Array<string | boolean>> {
ctx.logger.info(`[oraclepool] Launching a batch of ${quantity} instances in group ${group.name}`);

const result = <string[]>[];

this.computeManagementClient.regionId = group.region;
const poolDetails = await this.computeManagementClient.getInstancePool({
instancePoolId: group.instanceConfigurationId,
Expand All @@ -86,25 +89,36 @@ export default class OracleInstancePoolManager implements CloudInstanceManager {
return instance.id;
});

ctx.logger.debug(`[oraclepool] Instance pool ${group.name} instances`, { instances: poolInstances.items });
const currentInstanceIds = currentInventory.map((instance) => {
return instance.instanceId;
});

const newSize = quantity + groupCurrentCount;
if (groupCurrentCount == poolDetails.instancePool.size) {
ctx.logger.debug(`[oraclepool] Instance pool ${group.name} size matches current count`, {
current: groupCurrentCount,
size: poolDetails.instancePool.size,
newSize,
// mark any instances not previously seen as being launched now
result.push(
...existingInstanceIds.filter((instanceId) => {
return !currentInstanceIds.includes(instanceId);
}),
);

ctx.logger.debug(`[oraclepool] Instance pool ${group.name} instances`, { instances: poolInstances.items });
if (result.length > 0) {
ctx.logger.warn(`[oraclepool] Found instances in pool not in inventory, marking as launched now`, {
result,
});
} else {
ctx.logger.error(`[oraclepool] Instance pool ${group.name} size DOES NOT matches current count`, {
current: groupCurrentCount,
size: poolDetails.instancePool.size,
}

// always use the group desired count for instance pools
const newSize = group.scalingOptions.desiredCount;
if (newSize == poolDetails.instancePool.size) {
// underlying pool size matches the desired count, so no need to update group
ctx.logger.info(`[oraclepool] Instance pool ${group.name} size matches desired count, no changes needed`, {
newSize,
});
return result;
}

if (this.isDryRun) {
ctx.logger.info(`[oracle] Dry run enabled, instance pool size change skipped`, { newSize });
ctx.logger.info(`[oraclepool] Dry run enabled, instance pool size change skipped`, { newSize });
} else {
const updateResult = await this.computeManagementClient.updateInstancePool({
instancePoolId: group.instanceConfigurationId,
Expand Down Expand Up @@ -142,13 +156,15 @@ export default class OracleInstancePoolManager implements CloudInstanceManager {
instancePoolId: group.instanceConfigurationId,
});

const result = newPoolInstances.items
.map((instance) => {
return instance.id;
})
.filter((instanceId) => {
return !existingInstanceIds.includes(instanceId);
});
result.push(
...newPoolInstances.items
.map((instance) => {
return instance.id;
})
.filter((instanceId) => {
return !existingInstanceIds.includes(instanceId);
}),
);

ctx.logger.info(`[oraclepool] Finished launching all the instances in group ${group.name}`, { result });

Expand Down
Loading

0 comments on commit d472ada

Please sign in to comment.