Skip to content

Commit 7d2ff8e

Browse files
committed
Add validation for retention lease construction (#37312)
This commit adds some simple validation that the values input to the retention lease constructor our valid values. We will later rely on these values being within the validated range.
1 parent da38262 commit 7d2ff8e

File tree

2 files changed

+56
-0
lines changed

2 files changed

+56
-0
lines changed

server/src/main/java/org/elasticsearch/index/seqno/RetentionLease.java

+6
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,12 @@ public String source() {
8181
* @param source the source of the retention lease
8282
*/
8383
public RetentionLease(final String id, final long retainingSequenceNumber, final long timestamp, final String source) {
84+
if (retainingSequenceNumber < SequenceNumbers.UNASSIGNED_SEQ_NO) {
85+
throw new IllegalArgumentException("retention lease retaining sequence number [" + retainingSequenceNumber + "] out of range");
86+
}
87+
if (timestamp < 0) {
88+
throw new IllegalArgumentException("retention lease timestamp [" + timestamp + "] out of range");
89+
}
8490
this.id = id;
8591
this.retainingSequenceNumber = retainingSequenceNumber;
8692
this.timestamp = timestamp;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/*
2+
* Licensed to Elasticsearch under one or more contributor
3+
* license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright
5+
* ownership. Elasticsearch licenses this file to you under
6+
* the Apache License, Version 2.0 (the "License"); you may
7+
* not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
20+
package org.elasticsearch.index.seqno;
21+
22+
import org.elasticsearch.test.ESTestCase;
23+
24+
import static org.elasticsearch.index.seqno.SequenceNumbers.UNASSIGNED_SEQ_NO;
25+
import static org.hamcrest.Matchers.containsString;
26+
import static org.hamcrest.Matchers.hasToString;
27+
28+
public class RetentionLeaseTests extends ESTestCase {
29+
30+
public void testRetainingSequenceNumberOutOfRange() {
31+
final long retainingSequenceNumber = randomLongBetween(Long.MIN_VALUE, UNASSIGNED_SEQ_NO - 1);
32+
final IllegalArgumentException e = expectThrows(
33+
IllegalArgumentException.class,
34+
() -> new RetentionLease("id", retainingSequenceNumber, randomNonNegativeLong(), "source"));
35+
assertThat(
36+
e,
37+
hasToString(containsString("retention lease retaining sequence number [" + retainingSequenceNumber + "] out of range")));
38+
}
39+
40+
public void testTimestampOutOfRange() {
41+
final long timestamp = randomLongBetween(Long.MIN_VALUE, -1);
42+
final IllegalArgumentException e = expectThrows(
43+
IllegalArgumentException.class,
44+
() -> new RetentionLease("id", randomLongBetween(SequenceNumbers.NO_OPS_PERFORMED, Long.MAX_VALUE), timestamp, "source"));
45+
assertThat(
46+
e,
47+
hasToString(containsString("retention lease timestamp [" + timestamp + "] out of range")));
48+
}
49+
50+
}

0 commit comments

Comments
 (0)