Skip to content
This repository has been archived by the owner on Nov 14, 2024. It is now read-only.

Commit

Permalink
Fix TimestampLeaseRequests serialization (#7421)
Browse files Browse the repository at this point in the history
  • Loading branch information
fsamuel-bs authored Nov 6, 2024
1 parent b186990 commit a83a02f
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 5 deletions.
1 change: 0 additions & 1 deletion atlasdb-commons-api/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ dependencies {
annotationProcessor 'org.immutables:value'

implementation 'com.fasterxml.jackson.core:jackson-annotations'
implementation 'com.fasterxml.jackson.core:jackson-databind'
implementation 'com.google.errorprone:error_prone_annotations'
implementation 'com.palantir.safe-logging:preconditions'

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,14 @@

package com.palantir.atlasdb.common.api.timelock;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonValue;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.palantir.logsafe.Preconditions;
import com.palantir.logsafe.Safe;
import com.palantir.logsafe.SafeArg;
import java.util.Comparator;
import org.immutables.value.Value;

@JsonDeserialize(as = ImmutableTimestampLeaseName.class)
@JsonSerialize(as = ImmutableTimestampLeaseName.class)
@Value.Immutable
@Safe
public interface TimestampLeaseName {
Expand All @@ -45,6 +42,7 @@ default void check() {
SafeArg.of("name", name()));
}

@JsonCreator(mode = JsonCreator.Mode.DELEGATING)
static TimestampLeaseName of(@Safe String name) {
return ImmutableTimestampLeaseName.builder().name(name).build();
}
Expand Down
5 changes: 5 additions & 0 deletions changelog/@unreleased/pr-7421.v2.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
type: fix
fix:
description: Fix `TimestampLeaseRequests` serialization and de-serialization.
links:
- https://github.com/palantir/atlasdb/pull/7421
2 changes: 2 additions & 0 deletions timelock-api/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ dependencies {
implementation 'com.google.guava:guava'

testImplementation project(':atlasdb-api')
testImplementation project(':atlasdb-commons-api')
testImplementation 'com.palantir.conjure.java.runtime:conjure-java-jackson-serialization'
testImplementation 'com.palantir.safe-logging:preconditions-assertj'
testImplementation 'org.assertj:assertj-core'
Expand All @@ -22,6 +23,7 @@ dependencies {
subprojects {
apply from: "../../gradle/shared.gradle"
dependencies {
api project(':atlasdb-commons-api')
api project(':lock-api-objects')
api project(':timelock-api')
api project(':leader-election-api')
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/*
* (c) Copyright 2024 Palantir Technologies Inc. All rights reserved.
*
* 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.palantir.atlasdb.timelock.api;

import static org.assertj.core.api.Assertions.assertThat;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.palantir.atlasdb.common.api.timelock.TimestampLeaseName;
import com.palantir.conjure.java.serialization.ObjectMappers;
import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import org.junit.jupiter.api.Test;

public class TimestampLeaseRequestsTest {
private static final ObjectMapper SERIALIZATION_MAPPER =
ObjectMappers.newClientObjectMapper().enable(SerializationFeature.INDENT_OUTPUT);
private static final ObjectMapper DESERIALIZATION_MAPPER =
ObjectMappers.newServerObjectMapper().enable(SerializationFeature.INDENT_OUTPUT);

@Test
void canSerializeAndDeserialize() throws IOException {
Namespace namespace1 = Namespace.of("timelock-client-namespace");
TimestampLeaseRequests leaseRequests1 = TimestampLeaseRequests.of(
RequestId.of(new UUID(0, 0)),
Map.of(
TimestampLeaseName.of("timestamp-lease-name-1"), 10,
TimestampLeaseName.of("timestamp-lease-name-2"), 20));

Namespace namespace2 = Namespace.of("another-timelock-client-namespace");
TimestampLeaseRequests leaseRequests2 = TimestampLeaseRequests.of(
RequestId.of(new UUID(0, 1)), Map.of(TimestampLeaseName.of("another-timestamp-lease-name"), 30));

MultiClientTimestampLeaseRequest request = MultiClientTimestampLeaseRequest.of(Map.of(
namespace1,
NamespaceTimestampLeaseRequest.of(List.of(leaseRequests1)),
namespace2,
NamespaceTimestampLeaseRequest.of(List.of(leaseRequests2))));
byte[] asBytes = SERIALIZATION_MAPPER.writeValueAsBytes(request);
MultiClientTimestampLeaseRequest deserialized =
DESERIALIZATION_MAPPER.readValue(asBytes, MultiClientTimestampLeaseRequest.class);
assertThat(deserialized).isEqualTo(request);
}
}

0 comments on commit a83a02f

Please sign in to comment.