-
Notifications
You must be signed in to change notification settings - Fork 1.1k
feat: Denylist small worker sizes in reverse replication #3044
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
8380eb3
Add check for small machines
manitgupta 577b781
Remove log
manitgupta 02c9148
Apply spotless
manitgupta 8cb6ff3
Use Preconditions
manitgupta 8553e75
Update IT and LT base classes with workerMachineType
manitgupta 10181e9
Add to params instead of env
manitgupta File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
77 changes: 77 additions & 0 deletions
77
...google/cloud/teleport/v2/spanner/migrations/utils/DataflowWorkerMachineTypeValidator.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,77 @@ | ||
| /* | ||
| * Copyright (C) 2025 Google LLC | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); you may not | ||
| * use this file except in compliance with the License. You may obtain a copy of | ||
| * the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | ||
| * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the | ||
| * License for the specific language governing permissions and limitations under | ||
| * the License. | ||
| */ | ||
| package com.google.cloud.teleport.v2.spanner.migrations.utils; | ||
|
|
||
| import com.google.common.base.Preconditions; | ||
| import org.apache.commons.lang3.StringUtils; | ||
|
|
||
| public class DataflowWorkerMachineTypeValidator { | ||
|
|
||
| public static void validateMachineSpecs(String workerMachineType, Integer minCPUs) { | ||
| Preconditions.checkArgument( | ||
| workerMachineType != null && !StringUtils.isBlank(workerMachineType), | ||
| "Policy Violation: You must specify a workerMachineType with at least %s vCPUs.", | ||
| minCPUs); | ||
|
|
||
| // Handle custom machine types first, format is custom-{vCPU}-{RAM} | ||
| if (workerMachineType.startsWith("custom-")) { | ||
| String[] parts = workerMachineType.split("-"); | ||
| Preconditions.checkArgument( | ||
| parts.length == 3, | ||
| "Invalid custom machine type format: '%s'. Expected format: custom-{vCPU}-{RAM}.", | ||
| workerMachineType); | ||
| Integer vCpus = null; | ||
| try { | ||
| vCpus = Integer.parseInt(parts[1]); | ||
| } catch (NumberFormatException e) { | ||
| Preconditions.checkArgument( | ||
| false, "Invalid vCPU number in custom machine type: '%s'", workerMachineType); | ||
| } | ||
| Preconditions.checkArgument( | ||
| vCpus >= minCPUs, | ||
| "Policy Violation: Custom machine type '%s' has %s vCPUs. Minimum allowed is %s. Please use a higher machine type.", | ||
| workerMachineType, | ||
| vCpus, | ||
| minCPUs); | ||
| } else { | ||
| // Handle standard machine types. | ||
| java.util.regex.Pattern pattern = java.util.regex.Pattern.compile(".*-(\\d+)$"); | ||
| java.util.regex.Matcher matcher = pattern.matcher(workerMachineType); | ||
|
|
||
| if (matcher.find()) { | ||
| Integer vCpus = null; | ||
| try { | ||
| vCpus = Integer.parseInt(matcher.group(1)); | ||
| } catch (NumberFormatException e) { | ||
| Preconditions.checkArgument( | ||
| false, "Invalid vCPU number in machine type: '%s'", workerMachineType); | ||
| } | ||
| Preconditions.checkArgument( | ||
| vCpus >= minCPUs, | ||
| "Policy Violation: Machine type '%s' has %s vCPUs. Minimum allowed is %s.", | ||
| workerMachineType, | ||
| vCpus, | ||
| minCPUs); | ||
| } else { | ||
| Preconditions.checkArgument( | ||
| false, | ||
| "Unknown machine type format: '%s'. Please use a standard machine type (e.g., n1-standard-4) or a custom machine type (e.g., custom-4-4096) with at least %s vCPUs.", | ||
| workerMachineType, | ||
| minCPUs); | ||
| } | ||
| } | ||
| } | ||
| } | ||
109 changes: 109 additions & 0 deletions
109
...le/cloud/teleport/v2/spanner/migrations/utils/DataflowWorkerMachineTypeValidatorTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,109 @@ | ||
| /* | ||
| * Copyright (C) 2025 Google LLC | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); you may not | ||
| * use this file except in compliance with the License. You may obtain a copy of | ||
| * the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | ||
| * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the | ||
| * License for the specific language governing permissions and limitations under | ||
| * the License. | ||
| */ | ||
| package com.google.cloud.teleport.v2.spanner.migrations.utils; | ||
|
|
||
| import static org.junit.Assert.assertThrows; | ||
|
|
||
| import org.junit.Test; | ||
| import org.junit.runner.RunWith; | ||
| import org.junit.runners.JUnit4; | ||
|
|
||
| @RunWith(JUnit4.class) | ||
| public class DataflowWorkerMachineTypeValidatorTest { | ||
|
|
||
| @Test | ||
| public void testValidMachineType() { | ||
| DataflowWorkerMachineTypeValidator.validateMachineSpecs("n1-standard-4", 4); | ||
| } | ||
|
|
||
| @Test | ||
| public void testValidMachineTypeHighCpu() { | ||
| DataflowWorkerMachineTypeValidator.validateMachineSpecs("n1-standard-8", 4); | ||
| } | ||
|
|
||
| @Test | ||
| public void testInvalidMachineTypeLowCpu() { | ||
| assertThrows( | ||
| IllegalArgumentException.class, | ||
| () -> { | ||
| DataflowWorkerMachineTypeValidator.validateMachineSpecs("n1-standard-2", 4); | ||
| }); | ||
| } | ||
|
|
||
| @Test | ||
| public void testNullMachineType() { | ||
| assertThrows( | ||
| IllegalArgumentException.class, | ||
| () -> { | ||
| DataflowWorkerMachineTypeValidator.validateMachineSpecs(null, 4); | ||
| }); | ||
| } | ||
|
|
||
| @Test | ||
| public void testEmptyMachineType() { | ||
| assertThrows( | ||
| IllegalArgumentException.class, | ||
| () -> { | ||
| DataflowWorkerMachineTypeValidator.validateMachineSpecs(" ", 4); | ||
| }); | ||
| } | ||
|
|
||
| @Test | ||
| public void testValidCustomMachineType() { | ||
| DataflowWorkerMachineTypeValidator.validateMachineSpecs("custom-8-12345", 4); | ||
| } | ||
|
|
||
| @Test | ||
| public void testValidCustomMachineTypeMinCpu() { | ||
| DataflowWorkerMachineTypeValidator.validateMachineSpecs("custom-4-12345", 4); | ||
| } | ||
|
|
||
| @Test | ||
| public void testInvalidCustomMachineTypeLowCpu() { | ||
| assertThrows( | ||
| IllegalArgumentException.class, | ||
| () -> { | ||
| DataflowWorkerMachineTypeValidator.validateMachineSpecs("custom-2-12345", 4); | ||
| }); | ||
| } | ||
|
|
||
| @Test | ||
| public void testInvalidCustomMachineTypeFormat() { | ||
| assertThrows( | ||
| IllegalArgumentException.class, | ||
| () -> { | ||
| DataflowWorkerMachineTypeValidator.validateMachineSpecs("custom-2", 4); | ||
| }); | ||
| } | ||
|
|
||
| @Test | ||
| public void testInvalidCustomMachineTypeNonNumericCpu() { | ||
| assertThrows( | ||
| IllegalArgumentException.class, | ||
| () -> { | ||
| DataflowWorkerMachineTypeValidator.validateMachineSpecs("custom-abc-12345", 4); | ||
| }); | ||
| } | ||
|
|
||
| @Test | ||
| public void testUnknownMachineType() { | ||
| assertThrows( | ||
| IllegalArgumentException.class, | ||
| () -> { | ||
| DataflowWorkerMachineTypeValidator.validateMachineSpecs("unknown-machine-type", 4); | ||
| }); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.