Skip to content

Commit

Permalink
Merge pull request #939 from golemfactory/bugfix/JST-942/options-cleanup
Browse files Browse the repository at this point in the history
Options refactoring
  • Loading branch information
SewerynKras authored May 27, 2024
2 parents 3e16fe6 + 3a878c8 commit e2133d2
Show file tree
Hide file tree
Showing 33 changed files with 203 additions and 224 deletions.
11 changes: 6 additions & 5 deletions examples/advanced/hello-world.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { DraftOfferProposalPool, GolemNetwork, DemandSpec } from "@golem-sdk/golem-js";
import { DraftOfferProposalPool, GolemNetwork, MarketOrderSpec } from "@golem-sdk/golem-js";

import { pinoPrettyLogger } from "@golem-sdk/pino-logger";

Expand All @@ -14,11 +14,12 @@ import { pinoPrettyLogger } from "@golem-sdk/pino-logger";
try {
await glm.connect();

const demand: DemandSpec = {
const order: MarketOrderSpec = {
demand: {
activity: {
workload: {
imageTag: "golem/alpine:latest",
},
expirationSec: 30 * 60,
},
market: {
maxAgreements: 1,
Expand All @@ -37,10 +38,10 @@ import { pinoPrettyLogger } from "@golem-sdk/pino-logger";
});

const allocation = await glm.payment.createAllocation({
budget: glm.market.estimateBudget(demand),
budget: glm.market.estimateBudget(order),
expirationSec: 60 * 60, // 60 minutes
});
const demandSpecification = await glm.market.buildDemandDetails(demand.demand, allocation);
const demandSpecification = await glm.market.buildDemandDetails(order.demand, allocation);
const proposal$ = glm.market.startCollectingProposals({
demandSpecification,
bufferSize: 15,
Expand Down
2 changes: 1 addition & 1 deletion examples/advanced/local-image/serveLocalGvmi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ const getImagePath = (path: string) => fileURLToPath(new URL(path, import.meta.u

const demand = {
demand: {
activity: {
workload: {
// Here you supply the path to the GVMI file that you want to deploy and use
// using the file:// protocol will make the SDK switch to "GVMI" serving mode
imageUrl: `file://${getImagePath("./alpine.gvmi")}`,
Expand Down
2 changes: 1 addition & 1 deletion examples/advanced/manual-pools.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const RENT_HOURS = 0.25;

const demandOptions = {
demand: {
activity: {
workload: {
imageTag: "golem/alpine:latest",
minCpuCores: 1,
minMemGib: 1,
Expand Down
8 changes: 4 additions & 4 deletions examples/basic/many-of.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@
* This example demonstrates how easily lease multiple machines at once.
*/

import { DemandSpec, GolemNetwork } from "@golem-sdk/golem-js";
import { GolemNetwork, MarketOrderSpec } from "@golem-sdk/golem-js";
import { pinoPrettyLogger } from "@golem-sdk/pino-logger";

const demand: DemandSpec = {
const order: MarketOrderSpec = {
demand: {
activity: { imageTag: "golem/alpine:latest" },
workload: { imageTag: "golem/alpine:latest" },
},
market: {
maxAgreements: 1,
Expand All @@ -33,7 +33,7 @@ const demand: DemandSpec = {
// create a pool that can grow up to 3 leases at the same time
const pool = await glm.manyOf({
concurrency: 3,
demand,
order,
});
await Promise.allSettled([
pool.withLease(async (lease) =>
Expand Down
8 changes: 4 additions & 4 deletions examples/basic/one-of.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { DemandSpec, GolemNetwork } from "@golem-sdk/golem-js";
import { MarketOrderSpec, GolemNetwork } from "@golem-sdk/golem-js";
import { pinoPrettyLogger } from "@golem-sdk/pino-logger";

const demandOptions: DemandSpec = {
const order: MarketOrderSpec = {
demand: {
activity: { imageTag: "golem/alpine:latest" },
workload: { imageTag: "golem/alpine:latest" },
},
market: {
maxAgreements: 1,
Expand All @@ -26,7 +26,7 @@ const demandOptions: DemandSpec = {

try {
await glm.connect();
const lease = await glm.oneOf(demandOptions);
const lease = await glm.oneOf(order);
await lease
.getExeUnit()
.then((exe) => exe.run("echo Hello, Golem! 👋"))
Expand Down
10 changes: 5 additions & 5 deletions examples/basic/vpn.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { DemandSpec, GolemNetwork } from "@golem-sdk/golem-js";
import { MarketOrderSpec, GolemNetwork } from "@golem-sdk/golem-js";
import { pinoPrettyLogger } from "@golem-sdk/pino-logger";

(async () => {
Expand All @@ -11,9 +11,9 @@ import { pinoPrettyLogger } from "@golem-sdk/pino-logger";
try {
await glm.connect();
const network = await glm.createNetwork({ ip: "192.168.7.0/24" });
const demand: DemandSpec = {
const order: MarketOrderSpec = {
demand: {
activity: { imageTag: "golem/alpine:latest" },
workload: { imageTag: "golem/alpine:latest" },
},
market: {
maxAgreements: 2,
Expand All @@ -27,10 +27,10 @@ import { pinoPrettyLogger } from "@golem-sdk/pino-logger";
},
network,
};
// create a pool that can grow up to 3 leases at the same time
// create a pool that can grow up to 2 leases at the same time
const pool = await glm.manyOf({
concurrency: 2,
demand,
order,
});
const lease1 = await pool.acquire();
const lease2 = await pool.acquire();
Expand Down
8 changes: 4 additions & 4 deletions examples/experimental/deployment/new-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ async function main() {
.createNetwork("basic", {
ip: "192.168.7.0/24",
})
.createActivityPool("app", {
.createLeaseProcessPool("app", {
demand: {
activity: {
workload: {
imageTag: "golem/node:latest",
},
},
Expand All @@ -43,9 +43,9 @@ async function main() {
network: "basic",
},
})
.createActivityPool("db", {
.createLeaseProcessPool("db", {
demand: {
activity: {
workload: {
imageTag: "golem/alpine:latest",
minCpuCores: 1,
minMemGib: 2,
Expand Down
8 changes: 4 additions & 4 deletions src/activity/work/work.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Activity, ActivityStateEnum, ExecutionConfig, IActivityApi, Result } from "../";
import { Activity, ActivityStateEnum, IActivityApi, Result } from "../";
import {
Capture,
Command,
Expand All @@ -24,7 +24,7 @@ import { TcpProxy } from "../../network/tcpProxy";
import { AgreementDTO } from "../../market/agreement/service";
import { ActivityApi } from "ya-ts-client";
import { YagnaExeScriptObserver } from "../../shared/yagna";
import { ExeScriptExecutor } from "../exe-script-executor";
import { ExecutionOptions, ExeScriptExecutor } from "../exe-script-executor";
import { INetworkApi } from "../../network/api";

export type Worker<OutputType> = (ctx: WorkContext) => Promise<OutputType>;
Expand All @@ -42,6 +42,7 @@ export interface WorkOptions {
logger?: Logger;
activityReadySetupFunctions?: Worker<unknown>[];
yagnaOptions?: YagnaOptions;
execution?: ExecutionOptions;
}

export interface CommandOptions {
Expand Down Expand Up @@ -78,7 +79,6 @@ export class WorkContext {
public readonly activity: Activity,
private readonly networkApi: INetworkApi,
private options?: WorkOptions,
executionOptions?: ExecutionConfig,
) {
this.activityPreparingTimeout = options?.activityPreparingTimeout || DEFAULTS.activityPreparingTimeout;
this.activityStateCheckingInterval = options?.activityStateCheckingInterval || DEFAULTS.activityStateCheckInterval;
Expand All @@ -93,7 +93,7 @@ export class WorkContext {
this.activityControl,
this.execObserver,
this.logger,
executionOptions,
this.options?.execution,
);
}

Expand Down
14 changes: 7 additions & 7 deletions src/experimental/deployment/builder.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ describe("Deployment builder", () => {
const builder = new GolemDeploymentBuilder(mockGolemNetwork);
expect(() => {
builder
.createActivityPool("my-pool", {
.createLeaseProcessPool("my-pool", {
demand: {
activity: {
workload: {
imageTag: "image",
minCpuCores: 1,
minMemGib: 1,
Expand All @@ -30,9 +30,9 @@ describe("Deployment builder", () => {
},
},
})
.createActivityPool("my-pool", {
.createLeaseProcessPool("my-pool", {
demand: {
activity: {
workload: {
imageTag: "image",
minCpuCores: 1,
minMemGib: 1,
Expand All @@ -50,7 +50,7 @@ describe("Deployment builder", () => {
},
},
});
}).toThrow(new GolemConfigError(`Activity pool with name my-pool already exists`));
}).toThrow(new GolemConfigError(`Lease Process Pool with name my-pool already exists`));
});
it("throws an error when creating a network with the same name", () => {
const builder = new GolemDeploymentBuilder(mockGolemNetwork);
Expand All @@ -71,9 +71,9 @@ describe("Deployment builder", () => {
.createNetwork("existing-network", {
id: "test",
})
.createActivityPool("my-pool", {
.createLeaseProcessPool("my-pool", {
demand: {
activity: { imageTag: "image", minCpuCores: 1, minMemGib: 1, minStorageGib: 1 },
workload: { imageTag: "image", minCpuCores: 1, minMemGib: 1, minStorageGib: 1 },
},
market: {
maxAgreements: 1,
Expand Down
22 changes: 8 additions & 14 deletions src/experimental/deployment/builder.ts
Original file line number Diff line number Diff line change
@@ -1,45 +1,39 @@
import { GolemConfigError } from "../../shared/error/golem-error";
import { NetworkOptions } from "../../network";
import { Deployment, DeploymentComponents } from "./deployment";
import { GolemNetwork } from "../../golem-network";
import { GolemNetwork, MarketOrderSpec } from "../../golem-network";
import { validateDeployment } from "./validate-deployment";
import { MarketOptions } from "../../market";
import { PaymentModuleOptions } from "../../payment";
import { BuildDemandOptions } from "../../market/demand";

interface DeploymentOptions {
replicas?: number | { min: number; max: number };
network?: string;
}

export interface CreateActivityPoolOptions {
demand: BuildDemandOptions;
market: MarketOptions;
export interface CreateLeaseProcessPoolOptions extends MarketOrderSpec {
deployment?: DeploymentOptions;
payment?: PaymentModuleOptions;
}

export class GolemDeploymentBuilder {
private components: DeploymentComponents = {
activityPools: [],
leaseProcessPools: [],
networks: [],
};

public reset() {
this.components = {
activityPools: [],
leaseProcessPools: [],
networks: [],
};
}

constructor(private glm: GolemNetwork) {}

createActivityPool(name: string, options: CreateActivityPoolOptions): this {
if (this.components.activityPools.some((pool) => pool.name === name)) {
throw new GolemConfigError(`Activity pool with name ${name} already exists`);
createLeaseProcessPool(name: string, options: CreateLeaseProcessPoolOptions): this {
if (this.components.leaseProcessPools.some((pool) => pool.name === name)) {
throw new GolemConfigError(`Lease Process Pool with name ${name} already exists`);
}

this.components.activityPools.push({ name, options });
this.components.leaseProcessPools.push({ name, options });

return this;
}
Expand Down
55 changes: 18 additions & 37 deletions src/experimental/deployment/deployment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@ import { GolemAbortError, GolemUserError } from "../../shared/error/golem-error"
import { defaultLogger, Logger, YagnaApi } from "../../shared/utils";
import { EventEmitter } from "eventemitter3";
import { ActivityModule } from "../../activity";
import { Network, NetworkOptions, NetworkModule } from "../../network";
import { Network, NetworkModule, NetworkOptions } from "../../network";
import { GftpStorageProvider, StorageProvider, WebSocketBrowserStorageProvider } from "../../shared/storage";
import { validateDeployment } from "./validate-deployment";
import { DemandBuildParams, DraftOfferProposalPool, MarketModule } from "../../market";
import { DraftOfferProposalPool, MarketModule } from "../../market";
import { PaymentModule } from "../../payment";
import { CreateActivityPoolOptions } from "./builder";
import { CreateLeaseProcessPoolOptions } from "./builder";
import { Subscription } from "rxjs";
import { LeaseProcessPool, LeaseProcessPoolOptions } from "../../lease-process";
import { LeaseProcessPool } from "../../lease-process";
import { DataTransferProtocol } from "../../shared/types";

export enum DeploymentState {
Expand Down Expand Up @@ -45,7 +45,7 @@ export interface DeploymentEvents {
}

export type DeploymentComponents = {
activityPools: { name: string; options: CreateActivityPoolOptions }[];
leaseProcessPools: { name: string; options: CreateLeaseProcessPoolOptions }[];
networks: { name: string; options: NetworkOptions }[];
};

Expand Down Expand Up @@ -156,10 +156,12 @@ export class Deployment {
});

// TODO: pass dataTransferProtocol to pool
for (const pool of this.components.activityPools) {
const { demandBuildOptions, leaseProcessPoolOptions } = this.prepareParams(pool.options);
for (const pool of this.components.leaseProcessPools) {
const network = pool.options?.deployment?.network
? this.networks.get(pool.options?.deployment.network)
: undefined;

const demandSpecification = await this.modules.market.buildDemandDetails(demandBuildOptions.demand, allocation);
const demandSpecification = await this.modules.market.buildDemandDetails(pool.options.demand, allocation);
const proposalPool = new DraftOfferProposalPool();

const proposalSubscription = this.modules.market
Expand All @@ -172,11 +174,14 @@ export class Deployment {
error: (e) => this.logger.error("Error while collecting proposals", e),
});

const leaseProcessPool = this.modules.market.createLeaseProcessPool(
proposalPool,
allocation,
leaseProcessPoolOptions,
);
const leaseProcessPool = this.modules.market.createLeaseProcessPool(proposalPool, allocation, {
replicas: pool.options.deployment?.replicas,
network,
leaseProcessOptions: {
activity: pool.options?.activity,
payment: pool.options?.payment,
},
});
this.pools.set(pool.name, {
proposalPool,
proposalSubscription,
Expand Down Expand Up @@ -244,28 +249,4 @@ export class Deployment {
await Promise.all(readyPools);
this.logger.info("Components deployed and ready to use");
}

private prepareParams(options: CreateActivityPoolOptions): {
demandBuildOptions: DemandBuildParams;
leaseProcessPoolOptions: LeaseProcessPoolOptions;
} {
const replicas =
typeof options.deployment?.replicas === "number"
? { min: options.deployment?.replicas, max: options.deployment?.replicas }
: typeof options.deployment?.replicas === "object"
? options.deployment?.replicas
: { min: 1, max: 1 };
const network = options.deployment?.network ? this.networks.get(options.deployment?.network) : undefined;
return {
demandBuildOptions: {
demand: options.demand,
market: options.market,
},
leaseProcessPoolOptions: {
agreementOptions: { invoiceFilter: options.payment?.invoiceFilter },
replicas,
network,
},
};
}
}
2 changes: 1 addition & 1 deletion src/experimental/deployment/validate-deployment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { DeploymentComponents } from "./deployment";

function validateNetworks(components: DeploymentComponents) {
const networkNames = new Set(components.networks.map((network) => network.name));
for (const pool of components.activityPools) {
for (const pool of components.leaseProcessPools) {
if (!pool.options.deployment?.network) {
continue;
}
Expand Down
Loading

0 comments on commit e2133d2

Please sign in to comment.