-
Notifications
You must be signed in to change notification settings - Fork 549
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fixup! update shardsizeexpression every 10 seconds
- Loading branch information
Showing
11 changed files
with
162 additions
and
151 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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
/* | ||
* Licensed to CRATE Technology GmbH ("Crate") under one or more contributor | ||
* license agreements. See the NOTICE file distributed with this work for | ||
* additional information regarding copyright ownership. Crate 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. | ||
* | ||
* However, if you have executed another commercial license agreement | ||
* with Crate these terms will supersede the license and you may use the | ||
* software solely pursuant to the terms of the relevant commercial agreement. | ||
*/ | ||
|
||
package io.crate.core; | ||
|
||
import java.util.concurrent.TimeUnit; | ||
|
||
/** | ||
* A cache for a single reference that is cached for <code>cacheTime</code> milliseconds | ||
* and after that refreshed by calling {@link #refresh()} on new access to {@link #get()}. | ||
* @param <T> | ||
*/ | ||
public abstract class CachedRef<T> { | ||
|
||
private final long cacheTime; | ||
private long cachedAt = 0L; | ||
private volatile T value = null; | ||
|
||
public CachedRef(long cacheTime, TimeUnit timeUnit) { | ||
this.cacheTime = timeUnit.toMillis(cacheTime); | ||
} | ||
|
||
/** | ||
* guaranteed to be not null if {@link #refresh()} does not return null. | ||
* @return the cached value or a refreshed one. | ||
*/ | ||
public T get() { | ||
ensureValue(); | ||
return value; | ||
} | ||
|
||
private synchronized void ensureValue() { | ||
long curTime = System.currentTimeMillis(); | ||
if (value == null || curTime - cachedAt > cacheTime) { | ||
value = refresh(); | ||
cachedAt = curTime; | ||
} | ||
} | ||
|
||
/** | ||
* create a new value | ||
* @return the new value | ||
*/ | ||
public abstract T refresh(); | ||
} |
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,71 @@ | ||
/* | ||
* Licensed to CRATE Technology GmbH ("Crate") under one or more contributor | ||
* license agreements. See the NOTICE file distributed with this work for | ||
* additional information regarding copyright ownership. Crate 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. | ||
* | ||
* However, if you have executed another commercial license agreement | ||
* with Crate these terms will supersede the license and you may use the | ||
* software solely pursuant to the terms of the relevant commercial agreement. | ||
*/ | ||
|
||
package io.crate.core; | ||
|
||
import com.carrotsearch.randomizedtesting.annotations.Repeat; | ||
import io.crate.test.integration.CrateUnitTest; | ||
import org.junit.Test; | ||
|
||
import java.util.concurrent.TimeUnit; | ||
import java.util.concurrent.atomic.AtomicLong; | ||
|
||
import static org.hamcrest.Matchers.greaterThan; | ||
import static org.hamcrest.Matchers.is; | ||
|
||
public class CachedRefTest extends CrateUnitTest { | ||
|
||
@Test | ||
public void testZeroCacheAlwaysRefresh() throws Exception { | ||
CachedRef<Long> longCache = new CachedRef<Long>(0, TimeUnit.MILLISECONDS) { | ||
private AtomicLong internalLong = new AtomicLong(0L); | ||
@Override | ||
public Long refresh() { | ||
return internalLong.incrementAndGet(); | ||
} | ||
}; | ||
long previous = -1L; | ||
for (int i = 0; i < 1000; i++) { | ||
long cur = longCache.get(); | ||
assertThat(cur, is(greaterThan(previous))); | ||
previous = cur; | ||
Thread.sleep(1); | ||
} | ||
} | ||
|
||
@Repeat(iterations=10) | ||
@Test | ||
public void testRefresh() throws Exception { | ||
CachedRef<Long> longCache = new CachedRef<Long>(10, TimeUnit.MILLISECONDS) { | ||
private AtomicLong internalLong = new AtomicLong(0L); | ||
@Override | ||
public Long refresh() { | ||
return internalLong.incrementAndGet(); | ||
} | ||
}; | ||
|
||
long first = longCache.get(); | ||
assertThat(first, is(1L)); | ||
assertThat(longCache.get(), is(first)); | ||
Thread.sleep(12); | ||
assertThat(longCache.get(), is(2L)); | ||
} | ||
} |
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
Oops, something went wrong.