-
Notifications
You must be signed in to change notification settings - Fork 151
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Test server Nexus endpoint operator apis (#2162)
* Bump API version to v1.36.0 * Nexus endpoint test server CRUD API implementation * cleanup * functional tests * test operator service external setup * test environment setup * test environment setup * skip functional tests with external server
- Loading branch information
Showing
8 changed files
with
732 additions
and
3 deletions.
There are no files selected for viewing
Submodule proto
updated
25 files
44 changes: 44 additions & 0 deletions
44
...al-test-server/src/main/java/io/temporal/internal/testservice/TestNexusEndpointStore.java
This file contains 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,44 @@ | ||
/* | ||
* Copyright (C) 2022 Temporal Technologies, Inc. All Rights Reserved. | ||
* | ||
* Copyright (C) 2012-2016 Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
* | ||
* Modifications copyright (C) 2017 Uber Technologies, Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this material 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 io.temporal.internal.testservice; | ||
|
||
import io.temporal.api.nexus.v1.Endpoint; | ||
import io.temporal.api.nexus.v1.EndpointSpec; | ||
import java.io.Closeable; | ||
import java.util.List; | ||
|
||
public interface TestNexusEndpointStore extends Closeable { | ||
|
||
Endpoint createEndpoint(EndpointSpec spec); | ||
|
||
Endpoint updateEndpoint(String id, long version, EndpointSpec spec); | ||
|
||
void deleteEndpoint(String id, long version); | ||
|
||
Endpoint getEndpoint(String id); | ||
|
||
List<Endpoint> listEndpoints(long pageSize, byte[] nextPageToken, String name); | ||
|
||
void validateEndpointSpec(EndpointSpec spec); | ||
|
||
@Override | ||
void close(); | ||
} |
189 changes: 189 additions & 0 deletions
189
...est-server/src/main/java/io/temporal/internal/testservice/TestNexusEndpointStoreImpl.java
This file contains 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,189 @@ | ||
/* | ||
* Copyright (C) 2022 Temporal Technologies, Inc. All Rights Reserved. | ||
* | ||
* Copyright (C) 2012-2016 Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
* | ||
* Modifications copyright (C) 2017 Uber Technologies, Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this material 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 io.temporal.internal.testservice; | ||
|
||
import io.grpc.Status; | ||
import io.temporal.api.nexus.v1.Endpoint; | ||
import io.temporal.api.nexus.v1.EndpointSpec; | ||
import java.util.*; | ||
import java.util.concurrent.ConcurrentSkipListMap; | ||
import java.util.regex.Pattern; | ||
import java.util.stream.Collectors; | ||
|
||
/** | ||
* TestNexusEndpointStoreImpl is an in-memory implementation of Nexus endpoint CRUD operations for | ||
* use with the test server. Because conflict resolution is not required, there is no handling for | ||
* created or updated timestamps. | ||
*/ | ||
public class TestNexusEndpointStoreImpl implements TestNexusEndpointStore { | ||
|
||
private static final Pattern ENDPOINT_NAME_REGEX = Pattern.compile("^[a-zA-Z_][a-zA-Z0-9_]*$"); | ||
|
||
private final SortedMap<String, Endpoint> endpoints = new ConcurrentSkipListMap<>(); | ||
private final Set<String> endpointNames = new HashSet<>(); | ||
|
||
@Override | ||
public Endpoint createEndpoint(EndpointSpec spec) { | ||
validateEndpointSpec(spec); | ||
|
||
if (!endpointNames.add(spec.getName())) { | ||
throw Status.ALREADY_EXISTS | ||
.withDescription("Nexus endpoint already registered with name: " + spec.getName()) | ||
.asRuntimeException(); | ||
} | ||
|
||
String id = UUID.randomUUID().toString(); | ||
Endpoint endpoint = Endpoint.newBuilder().setId(id).setVersion(1).setSpec(spec).build(); | ||
|
||
if (endpoints.putIfAbsent(id, endpoint) != null) { | ||
// This should never happen in practice | ||
throw Status.ALREADY_EXISTS | ||
.withDescription("Nexus endpoint already exists with ID: " + id) | ||
.asRuntimeException(); | ||
} | ||
|
||
return endpoint; | ||
} | ||
|
||
@Override | ||
public Endpoint updateEndpoint(String id, long version, EndpointSpec spec) { | ||
validateEndpointSpec(spec); | ||
|
||
Endpoint prev = endpoints.get(id); | ||
|
||
if (prev == null) { | ||
throw Status.NOT_FOUND | ||
.withDescription("Could not find Nexus endpoint with ID: " + id) | ||
.asRuntimeException(); | ||
} | ||
|
||
if (prev.getVersion() != version) { | ||
throw Status.INVALID_ARGUMENT | ||
.withDescription( | ||
"Error updating Nexus endpoint: version mismatch." | ||
+ " Expected: " | ||
+ prev.getVersion() | ||
+ " Received: " | ||
+ version) | ||
.asRuntimeException(); | ||
} | ||
|
||
if (!prev.getSpec().getName().equals(spec.getName()) && !endpointNames.add(spec.getName())) { | ||
throw Status.ALREADY_EXISTS | ||
.withDescription( | ||
"Error updating Nexus endpoint: " | ||
+ "endpoint already registered with updated name: " | ||
+ spec.getName()) | ||
.asRuntimeException(); | ||
} else { | ||
endpointNames.remove(prev.getSpec().getName()); | ||
} | ||
|
||
Endpoint updated = Endpoint.newBuilder(prev).setVersion(version + 1).setSpec(spec).build(); | ||
|
||
endpoints.put(id, updated); | ||
return updated; | ||
} | ||
|
||
@Override | ||
public void deleteEndpoint(String id, long version) { | ||
Endpoint existing = endpoints.get(id); | ||
|
||
if (existing == null) { | ||
throw Status.NOT_FOUND | ||
.withDescription("Could not find Nexus endpoint with ID: " + id) | ||
.asRuntimeException(); | ||
} | ||
|
||
if (existing.getVersion() != version) { | ||
throw Status.INVALID_ARGUMENT | ||
.withDescription( | ||
"Error deleting Nexus endpoint: version mismatch." | ||
+ " Expected " | ||
+ existing.getVersion() | ||
+ " Received: " | ||
+ version) | ||
.asRuntimeException(); | ||
} | ||
|
||
endpoints.remove(id); | ||
} | ||
|
||
@Override | ||
public Endpoint getEndpoint(String id) { | ||
Endpoint endpoint = endpoints.get(id); | ||
if (endpoint == null) { | ||
throw Status.NOT_FOUND | ||
.withDescription("Could not find Nexus endpoint with ID: " + id) | ||
.asRuntimeException(); | ||
} | ||
return endpoint; | ||
} | ||
|
||
@Override | ||
public List<Endpoint> listEndpoints(long pageSize, byte[] nextPageToken, String name) { | ||
if (name != null && !name.isEmpty()) { | ||
return endpoints.values().stream() | ||
.filter(ep -> ep.getSpec().getName().equals(name)) | ||
.limit(1) | ||
.collect(Collectors.toList()); | ||
} | ||
|
||
if (nextPageToken.length > 0) { | ||
return endpoints.tailMap(new String(nextPageToken)).values().stream() | ||
.skip(1) | ||
.limit(pageSize) | ||
.collect(Collectors.toList()); | ||
} | ||
return endpoints.values().stream().limit(pageSize).collect(Collectors.toList()); | ||
} | ||
|
||
@Override | ||
public void validateEndpointSpec(EndpointSpec spec) { | ||
if (spec.getName().isEmpty()) { | ||
throw Status.INVALID_ARGUMENT | ||
.withDescription("Nexus endpoint name cannot be empty") | ||
.asRuntimeException(); | ||
} | ||
if (!ENDPOINT_NAME_REGEX.matcher(spec.getName()).matches()) { | ||
throw Status.INVALID_ARGUMENT | ||
.withDescription( | ||
"Nexus endpoint name (" | ||
+ spec.getName() | ||
+ ") does not match expected pattern: " | ||
+ ENDPOINT_NAME_REGEX.pattern()) | ||
.asRuntimeException(); | ||
} | ||
if (!spec.hasTarget()) { | ||
throw Status.INVALID_ARGUMENT | ||
.withDescription("Nexus endpoint spec must have a target") | ||
.asRuntimeException(); | ||
} | ||
if (!spec.getTarget().hasWorker()) { | ||
throw Status.INVALID_ARGUMENT | ||
.withDescription("Test server only supports Nexus endpoints with worker targets") | ||
.asRuntimeException(); | ||
} | ||
} | ||
|
||
@Override | ||
public void close() {} | ||
} |
This file contains 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 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
Oops, something went wrong.