Skip to content
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

[OBSOLETE] Added API handler functions (scan and get) for LRO #15408

Closed
wants to merge 32 commits into from

Conversation

adrikagupta
Copy link
Contributor

  • Added scanOperations and getOperationRun API handler functions in OperationHttpHandler.java
  • Added getOperationRun() in OperationLifecycleManager.java
  • Added unit tests in the OperationLifecycleManagerTest.java
  • Manual testing for OperationHttpHandler is in progress

@adrikagupta adrikagupta requested a review from samdgupi November 7, 2023 12:52
@adrikagupta adrikagupta changed the title Added API handler functions (scan and get) for LRO [CDAP-20874] API handler functions (scan and get) for LRO Nov 7, 2023
@adrikagupta adrikagupta changed the title [CDAP-20874] API handler functions (scan and get) for LRO [CDAP-20874] Added API handler functions (scan and get) for LRO Nov 7, 2023
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add .vscode to local gitignore so that this files are not committed

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure. Done

responder,
OPERATIONS_LIST_PAGINATED_KEY,
jsonListResponder -> {
AtomicReference<OperationRun> lastRun = new AtomicReference<>(null);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no need to pass null

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

if (filter != null && !filter.isEmpty()) {
OperationType operationType = null;
OperationRunStatus operationStatus = null;
Pattern typePattern = Pattern.compile("type=(\\w+)");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we please convert this to a function which parses pattern of key=val(ANDkey=val)* then returns the key,val pairs. Then we can match the key to type/status using switch case. if there is a unknown key we throw exception.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure. Done

operationType = OperationType.valueOf(typeStr.toUpperCase());
}
while (statusMatcher.find()) {
String statusStr = statusMatcher.group(1);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

throw error if not valid OperationStatus/OperationType

return builder.build();
}

private void validateNamespace(@Nullable String namespaceId) throws BadRequestException {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: move after public methods

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

/**
* Service that manages lifecycle of Operation.
*/
/** Service that manages lifecycle of Operation. */
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please remove all formatting changes

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

public class OperationLifecycleManager {

private final TransactionRunner transactionRunner;
private static final AtomicInteger sourceId = new AtomicInteger();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why this are added ? this are not being used anywhere

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My bad. That time I was trying to do manual testing but forgot to remove it later. I'll double check before pushing the changes

responder.sendJson(HttpResponseStatus.OK, GSON.toJson(runs));
@QueryParam("filter") String filter)
throws BadRequestException, IOException {
validateNamespace(namespaceId);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add a todo for rbac check with a corresponding jira

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, I think these handlers should have a feature flag check.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Got it. I have added the check for this feature flag: SOURCE_CONTROL_MANAGEMENT_MULTI_APP


// if (latestOnly != null) {
// uri += ("&latestOnly=" + latestOnly);
// }
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove commented lines

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right. done

return detail;
}

private static List<OperationRunDetail> insertTestRuns() throws Exception {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is same in store also. we can separate this into a OperationTestBase class which in turn extends AppFabricTestBase

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done. Thank you for the suggestion :)

@@ -0,0 +1,364 @@
{
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please remove the .vscode folder from the PR.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes done

responder.sendJson(HttpResponseStatus.OK, GSON.toJson(runs));
@QueryParam("filter") String filter)
throws BadRequestException, IOException {
validateNamespace(namespaceId);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, I think these handlers should have a feature flag check.

Comment on lines 226 to 230
private void validateNamespace(@Nullable String namespaceId) throws BadRequestException {
if (namespaceId == null) {
throw new BadRequestException("Path parameter namespaceId cannot be empty");
}
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are more cases that need to be validated here, like in this method.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Got it. I have added more cases


@ClassRule public static final TemporaryFolder TEMP_FOLDER = new TemporaryFolder();

private static EmbeddedPostgres pg;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please rename this variable for better readability. Possibly postgres or psql.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure. Thank you for the correction

samdgupi and others added 2 commits November 9, 2023 15:42
Fix the null pointer exception caused by unsafe unboxing of Boolean
return new OperationRunFilter(operationType, operationStatus);
}

private static Map<String, String> parseFilter(String filter) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: change the name to parseFilterStr

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes done. I have edited this function and made it more generic

OperationRunStatus operationStatus = null;

for (Map.Entry<String, String> entry : filterKeyValMap.entrySet()) {
String key = entry.getKey();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

filterKey


for (Map.Entry<String, String> entry : filterKeyValMap.entrySet()) {
String key = entry.getKey();
String value = entry.getValue();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

filterValue

String value = entry.getValue();

switch (key) {
case "type":
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should add a enum for this

OpreationFilterKey{
TYPE
STATUS
}

We convert the key string to the enum. After that we can use switch case

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes right. Thank you for the correction

return new OperationRunFilter(operationType, operationStatus);
}

private static Map<String, String> parseFilter(String filter) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add a javadoc explaining the parsing logic

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes sure

Map<String, String> filterKeyValMap = new HashMap<>();
String[] filterKeyValPairs = filter.split("AND");

for (String keyValPair : filterKeyValPairs) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you can use regex to parse this easily

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right. Done

*
* @param runId run id of the operation to be fetched
* @return operation run detail of the operation to be fetched. If not found,
* OperationRunNotFoundException is thrown.S
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove S

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also use @throws for documenting exception

@@ -338,4 +338,4 @@ void clearData() throws IOException {
SMALLEST_POSSIBLE_STRING)),
Range.Bound.INCLUSIVE));
}
}
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove this change

@@ -69,7 +69,10 @@ protected void configure() {
}

@AfterClass
public static void afterClass() throws IOException {
public static void afterClass(){
try{
pg.close();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you can use Colsables.closequitely

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

@@ -1,3 +1,4 @@
.vscode/
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add in a seperate section similar to intellij files

@@ -98,4 +176,115 @@ public void failOperation(FullHttpRequest request, HttpResponder responder,
String.format("Updated status for operation run %s in namespace '%s'.", runId,
namespaceId));
}

private ScanOperationRunsRequest getScanRequest(
String namespaceId, String pageToken, Integer pageSize, String filter) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

filterStr

return builder.build();
}

private OperationRunFilter getFilter(String filter) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add a TODO for adding unit test for this

return builder.build();
}

private OperationRunFilter getFilter(String filter) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add this throes IllegalArgumentException

case TYPE:
try {
operationType = OperationType.valueOf(filterValue.toUpperCase());
} catch (IllegalArgumentException e) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No need to catch and rethrow it

return keyValMap;
}

private void validateNamespace(@Nullable String namespace) throws BadRequestException, NamespaceNotFoundException, AccessException{
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I dont think this can be shorthened to

  private NamespaceId validateNamespaceId(String namespaceId) throws BadRequestException {
    try {
      return new NamespaceId(namespaceId);
    } catch (IllegalArgumentException e) {
      throw new BadRequestException(e.getMessage(), e);
    }
  }

see SourceControlManagementHttpHandler for reference

@adrikagupta adrikagupta changed the title [CDAP-20874] Added API handler functions (scan and get) for LRO [OBSELETE] Added API handler functions (scan and get) for LRO Nov 16, 2023
@adrikagupta adrikagupta changed the title [OBSELETE] Added API handler functions (scan and get) for LRO [OBSOLETE] Added API handler functions (scan and get) for LRO Nov 16, 2023
@samdgupi samdgupi closed this May 2, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants