Skip to content

Commit 5df1978

Browse files
author
Erluo Li
committed
Set rest.li 2.0 as the default protocol used by client requests.
RB=555314
1 parent 1fd755c commit 5df1978

File tree

15 files changed

+80
-23
lines changed

15 files changed

+80
-23
lines changed

restli-client-testutils/src/main/java/com/linkedin/restli/client/testutils/MockBatchEntityResponseFactory.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,7 @@ private static <K, V extends RecordTemplate> DataMap buildDataMap(Map<K, V> reco
209209
DataMap rawErrorData = new DataMap();
210210
for (Map.Entry<K, ErrorResponse> errorResponse : errorResponses.entrySet())
211211
{
212-
rawErrorData.put(String.valueOf(errorResponse.getKey()), errorResponse.getValue().data());
212+
rawErrorData.put(URIParamUtils.encodeKeyForBody(errorResponse.getKey(), false, version), errorResponse.getValue().data());
213213
}
214214
batchResponseDataMap.put(BatchResponse.ERRORS, rawErrorData);
215215
return batchResponseDataMap;

restli-client-testutils/src/main/java/com/linkedin/restli/client/testutils/MockBatchKVResponseFactory.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ private static <K, V extends RecordTemplate> DataMap buildDataMap(Map<K, V> reco
173173
DataMap rawErrorData = new DataMap();
174174
for (Map.Entry<K, ErrorResponse> errorResponse : errorResponses.entrySet())
175175
{
176-
rawErrorData.put(String.valueOf(errorResponse.getKey()), errorResponse.getValue().data());
176+
rawErrorData.put(URIParamUtils.encodeKeyForBody(errorResponse.getKey(), false, version), errorResponse.getValue().data());
177177
}
178178
batchResponseDataMap.put(BatchResponse.ERRORS, rawErrorData);
179179
return batchResponseDataMap;

restli-client/src/main/java/com/linkedin/restli/client/ProtocolVersionOption.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,15 @@ public enum ProtocolVersionOption
3838
*/
3939
FORCE_USE_LATEST,
4040

41+
/**
42+
* Use the previous version of the Rest.li protocol to encode requests, regardless of the version running on the server.
43+
* The prev version of the Rest.li protocol is the deprecated version. This option should typically NOT be used for
44+
* production services.
45+
* CAUTION: this can cause requests to fail if the server does not understand the prev version of the protocol.
46+
* "Next version" is defined as {@link com.linkedin.restli.internal.common.AllProtocolVersions#PREVIOUS_PROTOCOL_VERSION}.
47+
*/
48+
FORCE_USE_PREVIOUS,
49+
4150
/**
4251
* Use the latest version of the Rest.li protocol if the server supports it.
4352
* If the server version is less than the baseline Rest.li protocol version then fail the request.

restli-client/src/main/java/com/linkedin/restli/client/RestClient.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@
2727
import com.linkedin.data.codec.PsonDataCodec;
2828
import com.linkedin.data.template.RecordTemplate;
2929
import com.linkedin.r2.filter.R2Constants;
30-
import com.linkedin.r2.filter.CompressionOption;
3130
import com.linkedin.r2.message.RequestContext;
3231
import com.linkedin.r2.message.rest.RestRequest;
3332
import com.linkedin.r2.message.rest.RestRequestBuilder;
@@ -50,7 +49,6 @@
5049

5150
import javax.mail.internet.ParseException;
5251
import java.io.IOException;
53-
import java.net.HttpCookie;
5452
import java.net.URI;
5553
import java.net.URISyntaxException;
5654
import java.util.Collections;
@@ -276,6 +274,7 @@ private ProtocolVersion getProtocolVersionForService(final Request<?> request)
276274
try
277275
{
278276
return getProtocolVersion(AllProtocolVersions.BASELINE_PROTOCOL_VERSION,
277+
AllProtocolVersions.PREVIOUS_PROTOCOL_VERSION,
279278
AllProtocolVersions.LATEST_PROTOCOL_VERSION,
280279
AllProtocolVersions.NEXT_PROTOCOL_VERSION,
281280
getAnnouncedVersion(_client.getMetadata(new URI(_uriPrefix + request.getServiceName()))),
@@ -335,6 +334,7 @@ private ProtocolVersion getProtocolVersionForService(final Request<?> request)
335334
* @return the {@link ProtocolVersion} that should be used to build the request
336335
*/
337336
/*package private*/static ProtocolVersion getProtocolVersion(ProtocolVersion baselineProtocolVersion,
337+
ProtocolVersion previousVersion,
338338
ProtocolVersion latestVersion,
339339
ProtocolVersion nextVersion,
340340
ProtocolVersion announcedVersion,
@@ -355,6 +355,8 @@ private ProtocolVersion getProtocolVersionForService(final Request<?> request)
355355
return nextVersion;
356356
case FORCE_USE_LATEST:
357357
return latestVersion;
358+
case FORCE_USE_PREVIOUS:
359+
return previousVersion;
358360
case USE_LATEST_IF_AVAILABLE:
359361
if (announcedVersion.compareTo(baselineProtocolVersion) == -1)
360362
{
@@ -628,6 +630,8 @@ private RestRequest buildMultiplexedRequest(MultiplexedRequest multiplexedReques
628630
RestRequestBuilder requestBuilder = new RestRequestBuilder(requestUri).setMethod(HttpMethod.POST.toString());
629631
addAcceptHeaders(requestBuilder, Collections.singletonList(AcceptType.JSON));
630632
addEntityAndContentTypeHeaders(requestBuilder, multiplexedRequest.getContent().data(), ContentType.JSON);
633+
//TODO: change this once multiplexer supports dynamic versioning.
634+
requestBuilder.setHeader(RestConstants.HEADER_RESTLI_PROTOCOL_VERSION, AllProtocolVersions.RESTLI_PROTOCOL_2_0_0.getProtocolVersion().toString());
631635
return requestBuilder.build();
632636
}
633637

restli-client/src/main/java/com/linkedin/restli/client/RestliRequestOptions.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,9 @@ public class RestliRequestOptions
4141
public static final RestliRequestOptions FORCE_USE_NEXT_OPTION =
4242
new RestliRequestOptions(ProtocolVersionOption.FORCE_USE_NEXT, null, null, null, null);
4343

44+
public static final RestliRequestOptions FORCE_USE_PREV_OPTION =
45+
new RestliRequestOptions(ProtocolVersionOption.FORCE_USE_PREVIOUS, null, null, null, null);
46+
4447
/**
4548
* Content type and accept types (if not null) passed in this constructor will take precedence over the corresponding configuration set
4649
* at {@link RestClient}. Note that this form of configuration at {@link RestClient} is deprecated, therefore please consider using
@@ -131,7 +134,7 @@ public boolean equals(Object o)
131134
@Override
132135
public int hashCode()
133136
{
134-
int result = _protocolVersionOption != null ? _protocolVersionOption.hashCode() : 0;
137+
int result = _protocolVersionOption.hashCode();
135138
result = 31 * result + (_requestCompressionOverride != null ? _requestCompressionOverride.hashCode() : 0);
136139
result = 31 * result + (_responseCompressionOverride != null ? _responseCompressionOverride.hashCode() : 0);
137140
result = 31 * result + (_contentType != null ? _contentType.hashCode() : 0);

restli-client/src/main/java/com/linkedin/restli/client/RestliRequestOptionsBuilder.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ public RestliRequestOptionsBuilder setContentType(RestClient.ContentType content
7070

7171
public RestliRequestOptionsBuilder setAcceptTypes(List<RestClient.AcceptType> acceptTypes)
7272
{
73-
_acceptTypes = Collections.unmodifiableList(acceptTypes);
73+
_acceptTypes = acceptTypes == null ? null : Collections.unmodifiableList(acceptTypes);
7474
return this;
7575
}
7676

restli-client/src/main/java/com/linkedin/restli/client/multiplexer/MultiplexedRequestBuilder.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import com.linkedin.restli.client.RestLiEncodingException;
3030
import com.linkedin.restli.client.uribuilders.RestliUriBuilderUtil;
3131
import com.linkedin.restli.common.ProtocolVersion;
32+
import com.linkedin.restli.common.RestConstants;
3233
import com.linkedin.restli.common.multiplexer.IndividualBody;
3334
import com.linkedin.restli.common.multiplexer.IndividualRequest;
3435
import com.linkedin.restli.common.multiplexer.IndividualRequestMap;

restli-client/src/test/java/com/linkedin/restli/client/TestVersionNegotiation.java

Lines changed: 33 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
import com.linkedin.restli.common.ProtocolVersion;
2020
import com.linkedin.restli.common.RestConstants;
21+
import com.linkedin.restli.internal.common.AllProtocolVersions;
2122
import org.testng.Assert;
2223
import org.testng.annotations.DataProvider;
2324
import org.testng.annotations.Test;
@@ -31,26 +32,29 @@
3132
*/
3233
public class TestVersionNegotiation
3334
{
34-
private static final ProtocolVersion _BASELINE_VERSION = new ProtocolVersion(1, 0, 0);
35-
private static final ProtocolVersion _LATEST_VERSION = new ProtocolVersion(2, 0, 0);
35+
private static final ProtocolVersion _BASELINE_VERSION = AllProtocolVersions.BASELINE_PROTOCOL_VERSION;
36+
private static final ProtocolVersion _PREV_VERSION = AllProtocolVersions.PREVIOUS_PROTOCOL_VERSION;
37+
private static final ProtocolVersion _LATEST_VERSION = new ProtocolVersion(3, 0, 0);
3638
private static final ProtocolVersion _NEXT_VERSION = new ProtocolVersion(3, 0, 0);
37-
39+
3840
@DataProvider(name = "data")
3941
public Object[][] getProtocolVersionClient()
4042
{
4143
ProtocolVersion lessThanDefaultVersion = new ProtocolVersion(0, 5, 0);
42-
ProtocolVersion betweenDefaultAndLatestVersion = new ProtocolVersion(1, 5, 0);
43-
ProtocolVersion greaterThanLatestVersion = new ProtocolVersion(2, 5, 0);
44+
ProtocolVersion betweenDefaultAndLatestVersion = new ProtocolVersion(2, 5, 0);
45+
ProtocolVersion greaterThanLatestVersion = new ProtocolVersion(3, 5, 0);
4446
ProtocolVersion greaterThanNextVersion = new ProtocolVersion(3, 5, 0);
4547

4648
/*
4749
Generate data to test the following function:
4850
getProtocolVersion(ProtocolVersion defaultVersion,
51+
ProtocolVersion previousVersion,
4952
ProtocolVersion latestVersion,
5053
ProtocolVersion nextVersion,
5154
ProtocolVersion announcedVersion,
5255
ProtocolVersionOption versionOption)
5356
*/
57+
5458
return new Object[][]
5559
{
5660
// baseline protocol "advertised" + graceful option => baseline protocol version
@@ -97,7 +101,22 @@ public Object[][] getProtocolVersionClient()
97101
{ betweenDefaultAndLatestVersion, ProtocolVersionOption.FORCE_USE_NEXT, _NEXT_VERSION },
98102

99103
// version greater than latest "advertised" + force next => next
100-
{ greaterThanLatestVersion, ProtocolVersionOption.FORCE_USE_NEXT, _NEXT_VERSION }
104+
{ greaterThanLatestVersion, ProtocolVersionOption.FORCE_USE_NEXT, _NEXT_VERSION },
105+
106+
// default version "advertised" + force next => next
107+
{_BASELINE_VERSION, ProtocolVersionOption.FORCE_USE_PREVIOUS, _PREV_VERSION },
108+
109+
// latest version "advertised" + force next => next
110+
{ _LATEST_VERSION, ProtocolVersionOption.FORCE_USE_PREVIOUS, _PREV_VERSION },
111+
112+
// next "advertised" + force next => next
113+
{ _NEXT_VERSION, ProtocolVersionOption.FORCE_USE_PREVIOUS, _PREV_VERSION },
114+
115+
// version between default and latest "advertised" + force next => next
116+
{ betweenDefaultAndLatestVersion, ProtocolVersionOption.FORCE_USE_PREVIOUS, _PREV_VERSION },
117+
118+
// version greater than latest "advertised" + force next => next
119+
{ greaterThanLatestVersion, ProtocolVersionOption.FORCE_USE_PREVIOUS, _PREV_VERSION }
101120
};
102121
}
103122

@@ -135,6 +154,7 @@ public void testProtocolVersionNegotiation(ProtocolVersion announcedVersion,
135154
ProtocolVersion expectedProtocolVersion)
136155
{
137156
Assert.assertEquals(RestClient.getProtocolVersion(_BASELINE_VERSION,
157+
_PREV_VERSION,
138158
_LATEST_VERSION,
139159
_NEXT_VERSION,
140160
announcedVersion,
@@ -149,6 +169,7 @@ public void testAnnouncedVersionLessThanBaseline()
149169
try
150170
{
151171
RestClient.getProtocolVersion(_BASELINE_VERSION,
172+
_PREV_VERSION,
152173
_LATEST_VERSION,
153174
_NEXT_VERSION,
154175
new ProtocolVersion(0, 0, 0),
@@ -171,21 +192,23 @@ public void testAnnouncedVersionWithVersionPercentages(ProtocolVersion versionIn
171192
properties.put(RestConstants.RESTLI_PROTOCOL_VERSION_PROPERTY, versionInput);
172193
properties.put(RestConstants.RESTLI_PROTOCOL_VERSION_PERCENTAGE_PROPERTY, versionPercentageInput);
173194
ProtocolVersion announcedVersion = RestClient.getAnnouncedVersion(properties);
174-
Assert.assertEquals(expectedAnnouncedVersion, announcedVersion);
195+
Assert.assertEquals(announcedVersion, expectedAnnouncedVersion);
175196
}
176197

177198
@DataProvider(name = "testForceUseNextVersionOverrideData")
178-
public Object[][] testForceUseNextVersionOverrideData()
199+
public Object[][] testForceUseVersionOverrideData()
179200
{
180201
return new Object[][]
181202
{
182203
{ProtocolVersionOption.FORCE_USE_NEXT, _NEXT_VERSION, true},
204+
{ProtocolVersionOption.FORCE_USE_PREVIOUS, _NEXT_VERSION, true},
183205
{ProtocolVersionOption.FORCE_USE_LATEST, _NEXT_VERSION, true},
184206
{ProtocolVersionOption.USE_LATEST_IF_AVAILABLE, _NEXT_VERSION, true},
185207

208+
{ProtocolVersionOption.FORCE_USE_PREVIOUS, _PREV_VERSION, false},
186209
{ProtocolVersionOption.FORCE_USE_NEXT, _NEXT_VERSION, false},
187210
{ProtocolVersionOption.FORCE_USE_LATEST, _LATEST_VERSION, false},
188-
{ProtocolVersionOption.USE_LATEST_IF_AVAILABLE, _LATEST_VERSION, false},
211+
{ProtocolVersionOption.USE_LATEST_IF_AVAILABLE, _BASELINE_VERSION, false},
189212
};
190213
}
191214

@@ -196,6 +219,7 @@ public void testForceUseNextVersionOverride(ProtocolVersionOption protocolVersio
196219
{
197220
ProtocolVersion announcedVersion = new ProtocolVersion("2.0.0");
198221
ProtocolVersion actualProtocolVersion = RestClient.getProtocolVersion(_BASELINE_VERSION,
222+
_PREV_VERSION,
199223
_LATEST_VERSION,
200224
_NEXT_VERSION,
201225
announcedVersion,

restli-client/src/test/java/com/linkedin/restli/client/multiplexer/MultiplexerTestBase.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
import com.linkedin.restli.common.HttpStatus;
3737
import com.linkedin.restli.common.ResourceMethod;
3838
import com.linkedin.restli.common.ResourceSpecImpl;
39+
import com.linkedin.restli.common.RestConstants;
3940
import com.linkedin.restli.common.multiplexer.IndividualBody;
4041
import com.linkedin.restli.common.multiplexer.IndividualRequest;
4142
import com.linkedin.restli.common.multiplexer.IndividualRequestMap;

restli-common/src/main/java/com/linkedin/restli/internal/common/AllProtocolVersions.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,19 @@ public enum AllProtocolVersions
2828
RESTLI_PROTOCOL_1_0_0(new ProtocolVersion(1, 0, 0)),
2929
RESTLI_PROTOCOL_2_0_0(new ProtocolVersion(2, 0, 0));
3030

31+
/** Used by the server to determine the oldest supported version */
3132
public static final ProtocolVersion OLDEST_SUPPORTED_PROTOCOL_VERSION = RESTLI_PROTOCOL_1_0_0.getProtocolVersion();
32-
public static final ProtocolVersion BASELINE_PROTOCOL_VERSION = RESTLI_PROTOCOL_1_0_0.getProtocolVersion();
33+
34+
/** Used by the client to force use a request to use the deprecated protocol. */
35+
public static final ProtocolVersion PREVIOUS_PROTOCOL_VERSION = RESTLI_PROTOCOL_1_0_0.getProtocolVersion();
36+
37+
/** Used by the client to use default protocol version. */
38+
public static final ProtocolVersion BASELINE_PROTOCOL_VERSION = RESTLI_PROTOCOL_2_0_0.getProtocolVersion();
39+
40+
/** Used by the server and client to use latest supported protocol. */
3341
public static final ProtocolVersion LATEST_PROTOCOL_VERSION = RESTLI_PROTOCOL_2_0_0.getProtocolVersion();
42+
43+
/** Used by the server and client to use development protocol. */
3444
public static final ProtocolVersion NEXT_PROTOCOL_VERSION = RESTLI_PROTOCOL_2_0_0.getProtocolVersion();
3545

3646
private final ProtocolVersion _protocolVersion;

0 commit comments

Comments
 (0)