-
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.
implement node level searchcontext registry
waiting for job-scoped shard sequence ids
- Loading branch information
Showing
13 changed files
with
298 additions
and
120 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
118 changes: 118 additions & 0 deletions
118
sql/src/main/java/io/crate/executor/transport/CollectContextService.java
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,118 @@ | ||
/* | ||
* 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.executor.transport; | ||
|
||
import com.carrotsearch.hppc.IntObjectMap; | ||
import com.carrotsearch.hppc.IntObjectOpenHashMap; | ||
import com.carrotsearch.hppc.cursors.ObjectCursor; | ||
import com.google.common.base.Throwables; | ||
import com.google.common.cache.*; | ||
import com.google.common.util.concurrent.UncheckedExecutionException; | ||
import org.elasticsearch.ElasticsearchException; | ||
import org.elasticsearch.common.Nullable; | ||
import org.elasticsearch.common.inject.Inject; | ||
import org.elasticsearch.common.lease.Releasable; | ||
import org.elasticsearch.common.lease.Releasables; | ||
import org.elasticsearch.common.logging.Loggers; | ||
import org.elasticsearch.common.settings.Settings; | ||
import org.elasticsearch.common.unit.TimeValue; | ||
import org.elasticsearch.search.internal.SearchContext; | ||
|
||
import javax.annotation.Nonnull; | ||
import java.util.UUID; | ||
import java.util.concurrent.Callable; | ||
import java.util.concurrent.ExecutionException; | ||
import java.util.concurrent.TimeUnit; | ||
import java.util.concurrent.locks.ReentrantLock; | ||
|
||
public class CollectContextService implements Releasable { | ||
|
||
public static final String EXPIRATION_SETTING = "collect.context_keep_alive"; | ||
public static final TimeValue EXPIRATION_DEFAULT = new TimeValue(5, TimeUnit.MINUTES); | ||
|
||
private final ReentrantLock lock; | ||
private final LoadingCache<UUID, IntObjectMap<SearchContext>> contexts; | ||
|
||
@Inject | ||
public CollectContextService(Settings settings) { | ||
long expirationMillis = settings.getAsTime(EXPIRATION_SETTING, EXPIRATION_DEFAULT).millis(); | ||
if (expirationMillis == 0) { | ||
Loggers.getLogger(getClass()).info("invalid {}. using default of {}", EXPIRATION_SETTING, EXPIRATION_DEFAULT); | ||
expirationMillis = EXPIRATION_DEFAULT.millis(); | ||
} | ||
this.contexts = CacheBuilder.newBuilder().expireAfterAccess( | ||
expirationMillis, | ||
TimeUnit.MILLISECONDS | ||
).removalListener(new RemovalListener<UUID, IntObjectMap<SearchContext>>() { | ||
@Override | ||
public void onRemoval(@Nonnull RemovalNotification<UUID, IntObjectMap<SearchContext>> notification) { | ||
for (ObjectCursor<SearchContext> cursor : notification.getValue().values()) { | ||
Releasables.close(cursor.value); | ||
} | ||
} | ||
}).build(new CacheLoader<UUID, IntObjectMap<SearchContext>>() { | ||
@Override | ||
public IntObjectMap<SearchContext> load(@Nonnull UUID key) throws Exception { | ||
return new IntObjectOpenHashMap<>(); | ||
} | ||
}); | ||
this.lock = new ReentrantLock(); | ||
} | ||
|
||
|
||
@Nullable | ||
public SearchContext getContext(UUID jobId, int searchContextId) { | ||
final IntObjectMap<SearchContext> searchContexts; | ||
try { | ||
searchContexts = contexts.get(jobId); | ||
} catch (ExecutionException|UncheckedExecutionException e) { | ||
throw Throwables.propagate(e); | ||
} | ||
return searchContexts.get(searchContextId); | ||
} | ||
|
||
public SearchContext getOrCreateContext(UUID jobId, | ||
int searchContextId, | ||
Callable<SearchContext> createSearchContextCallable) { | ||
try { | ||
final IntObjectMap<SearchContext> searchContexts = contexts.get(jobId); | ||
lock.lock(); | ||
try { | ||
SearchContext searchContext = searchContexts.get(searchContextId); | ||
if (searchContext == null) { | ||
searchContext = createSearchContextCallable.call(); | ||
searchContexts.put(searchContextId, searchContext); | ||
} | ||
return searchContext; | ||
} finally { | ||
lock.unlock(); | ||
} | ||
} catch (Exception e) { | ||
throw Throwables.propagate(e); | ||
} | ||
} | ||
|
||
@Override | ||
public void close() throws ElasticsearchException { | ||
this.contexts.invalidateAll(); | ||
} | ||
} |
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
Oops, something went wrong.