-
Notifications
You must be signed in to change notification settings - Fork 73
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Extensions] Add all fields of LockModel to RestGetLockAction response (
#342) * Modifies the RestGetLockAction response to include all fields of the lock model object, rather than the lock model object itself Signed-off-by: Joshua Palis <[email protected]> * Removing unused fields Signed-off-by: Joshua Palis <[email protected]> * reverting field removal Signed-off-by: Joshua Palis <[email protected]> * Adding lockID to back to response Signed-off-by: Joshua Palis <[email protected]> * Adding checks to multinode get lock rest integration tests to ensure that all response fields are populated Signed-off-by: Joshua Palis <[email protected]> * fixing lock time parsing Signed-off-by: Joshua Palis <[email protected]> * Adds a new Response class to house serde logic for RestGetLockAction response Signed-off-by: Joshua Palis <[email protected]> * Moving parser.nextToken() within AcquireLockResponse.parse() Signed-off-by: Joshua Palis <[email protected]> * Implementing toXContentObject for AcquireLockRequest Signed-off-by: Joshua Palis <[email protected]> * Moving AcquireLockResponse to transpor package Signed-off-by: Joshua Palis <[email protected]> --------- Signed-off-by: Joshua Palis <[email protected]>
- Loading branch information
Showing
4 changed files
with
145 additions
and
27 deletions.
There are no files selected for viewing
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
99 changes: 99 additions & 0 deletions
99
src/main/java/org/opensearch/jobscheduler/transport/AcquireLockResponse.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,99 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* | ||
* The OpenSearch Contributors require contributions made to | ||
* this file be licensed under the Apache-2.0 license or a | ||
* compatible open source license. | ||
*/ | ||
package org.opensearch.jobscheduler.transport; | ||
|
||
import java.io.IOException; | ||
|
||
import org.opensearch.common.xcontent.XContentParserUtils; | ||
import org.opensearch.core.xcontent.ToXContentObject; | ||
import org.opensearch.core.xcontent.XContentBuilder; | ||
import org.opensearch.core.xcontent.XContentParser; | ||
import org.opensearch.jobscheduler.spi.LockModel; | ||
|
||
import static java.util.Objects.requireNonNull; | ||
|
||
/** | ||
* Response class used to facilitate serialization/deserialization of the GetLock response | ||
*/ | ||
public class AcquireLockResponse implements ToXContentObject { | ||
private final LockModel lock; | ||
private final String lockId; | ||
private final long seqNo; | ||
private final long primaryTerm; | ||
|
||
public AcquireLockResponse(final LockModel lock, final String lockId, final long seqNo, final long primaryTerm) { | ||
this.lock = lock; | ||
this.lockId = lockId; | ||
this.seqNo = seqNo; | ||
this.primaryTerm = primaryTerm; | ||
} | ||
|
||
public LockModel getLock() { | ||
return this.lock; | ||
} | ||
|
||
public String getLockId() { | ||
return this.lockId; | ||
} | ||
|
||
public long getSeqNo() { | ||
return this.seqNo; | ||
} | ||
|
||
public long getPrimaryTerm() { | ||
return this.primaryTerm; | ||
} | ||
|
||
public static AcquireLockResponse parse(final XContentParser parser) throws IOException { | ||
LockModel lock = null; | ||
String lockId = null; | ||
Long seqNo = null; | ||
Long primaryTerm = null; | ||
|
||
XContentParserUtils.ensureExpectedToken(XContentParser.Token.START_OBJECT, parser.nextToken(), parser); | ||
while (!XContentParser.Token.END_OBJECT.equals(parser.nextToken())) { | ||
String fieldName = parser.currentName(); | ||
parser.nextToken(); | ||
switch (fieldName) { | ||
case LockModel.LOCK_ID: | ||
lockId = parser.text(); | ||
break; | ||
case LockModel.SEQUENCE_NUMBER: | ||
seqNo = parser.longValue(); | ||
break; | ||
case LockModel.PRIMARY_TERM: | ||
primaryTerm = parser.longValue(); | ||
break; | ||
case LockModel.LOCK_MODEL: | ||
lock = LockModel.parse(parser, seqNo, primaryTerm); | ||
break; | ||
default: | ||
throw new IllegalArgumentException("Unknown field " + fieldName); | ||
} | ||
} | ||
return new AcquireLockResponse( | ||
requireNonNull(lock, "LockModel cannot be null"), | ||
requireNonNull(lockId, "LockId cannot be null"), | ||
requireNonNull(seqNo, "Sequence Number cannot be null"), | ||
requireNonNull(primaryTerm, "Primary Term cannot be null") | ||
); | ||
} | ||
|
||
@Override | ||
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { | ||
builder.startObject(); | ||
builder.field(LockModel.LOCK_ID, lockId); | ||
builder.field(LockModel.SEQUENCE_NUMBER, seqNo); | ||
builder.field(LockModel.PRIMARY_TERM, primaryTerm); | ||
builder.field(LockModel.LOCK_MODEL, lock); | ||
builder.endObject(); | ||
return builder; | ||
} | ||
|
||
} |
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