-
Notifications
You must be signed in to change notification settings - Fork 6.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Get reloaded metadata asynchronously after rule configuration altered (…
- Loading branch information
1 parent
e9e5b9e
commit 2849b62
Showing
5 changed files
with
130 additions
and
15 deletions.
There are no files selected for viewing
62 changes: 62 additions & 0 deletions
62
mode/core/src/main/java/org/apache/shardingsphere/mode/metadata/MetaDataContextHolder.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,62 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF 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.apache.shardingsphere.mode.metadata; | ||
|
||
import lombok.AllArgsConstructor; | ||
import lombok.Getter; | ||
import lombok.extern.slf4j.Slf4j; | ||
|
||
import java.util.concurrent.CompletableFuture; | ||
import java.util.concurrent.TimeUnit; | ||
import java.util.concurrent.atomic.AtomicReference; | ||
|
||
/** | ||
* Meta data context holder. | ||
*/ | ||
@AllArgsConstructor | ||
@Slf4j | ||
public final class MetaDataContextHolder { | ||
|
||
@Getter | ||
private final AtomicReference<MetaDataContexts> metaDataContexts; | ||
|
||
private final CompletableFuture<MetaDataContexts> future = new CompletableFuture<>(); | ||
|
||
/** | ||
* Get meta data contexts. | ||
* | ||
* @return meta data contexts | ||
*/ | ||
public MetaDataContexts getMetaDataContextsAsync() { | ||
try { | ||
return future.get(5, TimeUnit.SECONDS); | ||
} catch (final InterruptedException | java.util.concurrent.ExecutionException | java.util.concurrent.TimeoutException ex) { | ||
return metaDataContexts.get(); | ||
} | ||
} | ||
|
||
/** | ||
* Update meta data contexts. | ||
* | ||
* @param reloadMetaDataContexts reload meta data contexts | ||
*/ | ||
public void updateMetaDataContextsAsync(final MetaDataContexts reloadMetaDataContexts) { | ||
metaDataContexts.set(reloadMetaDataContexts); | ||
future.complete(reloadMetaDataContexts); | ||
} | ||
} |
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
46 changes: 46 additions & 0 deletions
46
...core/src/test/java/org/apache/shardingsphere/mode/metadata/MetaDataContextHolderTest.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,46 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF 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.apache.shardingsphere.mode.metadata; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
import java.util.concurrent.atomic.AtomicReference; | ||
|
||
import static org.hamcrest.CoreMatchers.is; | ||
import static org.hamcrest.MatcherAssert.assertThat; | ||
import static org.junit.jupiter.api.Assertions.assertNotEquals; | ||
import static org.mockito.Mockito.mock; | ||
|
||
class MetaDataContextHolderTest { | ||
|
||
private final MetaDataContexts metaDataContexts = mock(MetaDataContexts.class); | ||
|
||
private final MetaDataContextHolder metaDataContextHolder = new MetaDataContextHolder(new AtomicReference<>(metaDataContexts)); | ||
|
||
@Test | ||
void assertGetMetaDataContextsAsync() { | ||
assertThat(metaDataContextHolder.getMetaDataContextsAsync(), is(metaDataContexts)); | ||
} | ||
|
||
@Test | ||
void assertUpdateMetaDataContextsAsync() { | ||
assertThat(metaDataContextHolder.getMetaDataContextsAsync(), is(metaDataContexts)); | ||
metaDataContextHolder.updateMetaDataContextsAsync(mock(MetaDataContexts.class)); | ||
assertNotEquals(metaDataContextHolder.getMetaDataContextsAsync(), metaDataContexts); | ||
} | ||
} |
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