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.
This commit is contained in:
Jason Tedor 2019-01-10 18:13:05 -08:00 committed by GitHub
parent 9fd4d6ba4b
commit edc95c8a8e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 56 additions and 0 deletions

View File

@ -81,6 +81,12 @@ public final class RetentionLease {
* @param source the source of the retention lease
*/
public RetentionLease(final String id, final long retainingSequenceNumber, final long timestamp, final String source) {
if (retainingSequenceNumber < SequenceNumbers.UNASSIGNED_SEQ_NO) {
throw new IllegalArgumentException("retention lease retaining sequence number [" + retainingSequenceNumber + "] out of range");
}
if (timestamp < 0) {
throw new IllegalArgumentException("retention lease timestamp [" + timestamp + "] out of range");
}
this.id = id;
this.retainingSequenceNumber = retainingSequenceNumber;
this.timestamp = timestamp;

View File

@ -0,0 +1,50 @@
/*
* Licensed to Elasticsearch under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch licenses this file to you 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 org.elasticsearch.index.seqno;
import org.elasticsearch.test.ESTestCase;
import static org.elasticsearch.index.seqno.SequenceNumbers.UNASSIGNED_SEQ_NO;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.hasToString;
public class RetentionLeaseTests extends ESTestCase {
public void testRetainingSequenceNumberOutOfRange() {
final long retainingSequenceNumber = randomLongBetween(Long.MIN_VALUE, UNASSIGNED_SEQ_NO - 1);
final IllegalArgumentException e = expectThrows(
IllegalArgumentException.class,
() -> new RetentionLease("id", retainingSequenceNumber, randomNonNegativeLong(), "source"));
assertThat(
e,
hasToString(containsString("retention lease retaining sequence number [" + retainingSequenceNumber + "] out of range")));
}
public void testTimestampOutOfRange() {
final long timestamp = randomLongBetween(Long.MIN_VALUE, -1);
final IllegalArgumentException e = expectThrows(
IllegalArgumentException.class,
() -> new RetentionLease("id", randomLongBetween(SequenceNumbers.NO_OPS_PERFORMED, Long.MAX_VALUE), timestamp, "source"));
assertThat(
e,
hasToString(containsString("retention lease timestamp [" + timestamp + "] out of range")));
}
}