kikocis/java-driver public
Changesets · Branches · Tags · Compare
| Changeset | 7518b703ac6326d0d7d40e6bd7958a0f273c1496f3b3155c87ee40294c659b1b |
|---|---|
| Author | Alexandre Dutra <alexandre.dutra@datastax.com> · 2016-05-11 16:01 +02:00 |
| Committer | olim7t <omichallat+github@gmail.com> · 2016-07-08 09:06 -07:00 |
| Parents | 66728a15bc91 |
8 files changed
modified changelog/README.md +1 −0
| @@ -18,6 +18,7 @@ | ||
| 18 | 18 | - [improvement] JAVA-1227: Document "SELECT *" issue with prepared statement. |
| 19 | 19 | - [bug] JAVA-1160: Fix NPE in VersionNumber.getPreReleaseLabels(). |
| 20 | 20 | - [improvement] JAVA-1126: Handle schema changes in Mapper. |
| 21 | +- [bug] JAVA-1193: Refresh token and replica metadata synchronously when schema is altered. | |
| 21 | 22 | |
| 22 | 23 | |
| 23 | 24 | ### 3.0.2 |
modified driver-core/src/main/java/com/datastax/driver/core/Cluster.java +9 −6
| @@ -1851,9 +1851,10 @@ | ||
| 1851 | 1851 | if (controlConnection.refreshNodeInfo(host)) { |
| 1852 | 1852 | logger.debug("Successful reconnection to {}, setting host UP", host); |
| 1853 | 1853 | try { |
| 1854 | - if (isHostAddition) | |
| 1854 | + if (isHostAddition) { | |
| 1855 | 1855 | onAdd(host, connection); |
| 1856 | - else | |
| 1856 | + submitNodeListRefresh(); | |
| 1857 | + } else | |
| 1857 | 1858 | onUp(host, connection); |
| 1858 | 1859 | } catch (InterruptedException e) { |
| 1859 | 1860 | Thread.currentThread().interrupt(); |
| @@ -2226,15 +2227,15 @@ | ||
| 2226 | 2227 | return schemaRefreshRequestDebouncer.eventReceived(request); |
| 2227 | 2228 | } |
| 2228 | 2229 | |
| 2229 | - void submitNodeListRefresh() { | |
| 2230 | + ListenableFuture<Void> submitNodeListRefresh() { | |
| 2230 | 2231 | logger.trace("Submitting node list and token map refresh"); |
| 2231 | - nodeListRefreshRequestDebouncer.eventReceived(new NodeListRefreshRequest()); | |
| 2232 | + return nodeListRefreshRequestDebouncer.eventReceived(new NodeListRefreshRequest()); | |
| 2232 | 2233 | } |
| 2233 | 2234 | |
| 2234 | - void submitNodeRefresh(InetSocketAddress address, HostEvent eventType) { | |
| 2235 | + ListenableFuture<Void> submitNodeRefresh(InetSocketAddress address, HostEvent eventType) { | |
| 2235 | 2236 | NodeRefreshRequest request = new NodeRefreshRequest(address, eventType); |
| 2236 | 2237 | logger.trace("Submitting node refresh: {}", request); |
| 2237 | - nodeRefreshRequestDebouncer.eventReceived(request); | |
| 2238 | + return nodeRefreshRequestDebouncer.eventReceived(request); | |
| 2238 | 2239 | } |
| 2239 | 2240 | |
| 2240 | 2241 | // refresh the schema using the provided connection, and notice the future with the provided resultset once done |
| @@ -2709,6 +2710,7 @@ | ||
| 2709 | 2710 | public void runMayThrow() throws Exception { |
| 2710 | 2711 | if (controlConnection.refreshNodeInfo(host)) { |
| 2711 | 2712 | onAdd(host, null); |
| 2713 | + submitNodeListRefresh(); | |
| 2712 | 2714 | } else { |
| 2713 | 2715 | logger.debug("Not enough info for {}, ignoring host", host); |
| 2714 | 2716 | } |
| @@ -2745,6 +2747,7 @@ | ||
| 2745 | 2747 | if (metadata.remove(host)) { |
| 2746 | 2748 | logger.info("Cassandra host {} removed", host); |
| 2747 | 2749 | onRemove(host); |
| 2750 | + submitNodeListRefresh(); | |
| 2748 | 2751 | } |
| 2749 | 2752 | } |
| 2750 | 2753 | }; |
modified driver-core/src/main/java/com/datastax/driver/core/ControlConnection.java +37 −29
| @@ -267,16 +267,15 @@ | ||
| 267 | 267 | |
| 268 | 268 | // We need to refresh the node list first so we know about the cassandra version of |
| 269 | 269 | // the node we're connecting to. |
| 270 | + // This will create the token map for the first time, but it will be incomplete | |
| 271 | + // due to the lack of keyspace information | |
| 270 | 272 | refreshNodeListAndTokenMap(connection, cluster, isInitialConnection, true); |
| 271 | 273 | |
| 274 | + // refresh schema will also update the token map again, | |
| 275 | + // this time with information about keyspaces | |
| 272 | 276 | logger.debug("[Control connection] Refreshing schema"); |
| 273 | 277 | refreshSchema(connection, null, null, null, null, cluster); |
| 274 | 278 | |
| 275 | - // We need to refresh the node list again; | |
| 276 | - // We want that because the token map was not properly initialized by the first call above, | |
| 277 | - // since it requires the list of keyspaces to be loaded. | |
| 278 | - refreshNodeListAndTokenMap(connection, cluster, false, false); | |
| 279 | - | |
| 280 | 279 | return connection; |
| 281 | 280 | } catch (BusyConnectionException e) { |
| 282 | 281 | connection.closeAsync().force(); |
| @@ -306,11 +305,6 @@ | ||
| 306 | 305 | if (c == null || c.isClosed()) |
| 307 | 306 | return; |
| 308 | 307 | refreshSchema(c, targetType, targetKeyspace, targetName, signature, cluster); |
| 309 | - // If we rebuild all from scratch or have an updated keyspace, rebuild the token map | |
| 310 | - // since some replication on some keyspace may have changed | |
| 311 | - if ((targetType == null || targetType == KEYSPACE)) { | |
| 312 | - cluster.submitNodeListRefresh(); | |
| 313 | - } | |
| 314 | 308 | } catch (ConnectionException e) { |
| 315 | 309 | logger.debug("[Control connection] Connection error while refreshing schema ({})", e.getMessage()); |
| 316 | 310 | signalError(); |
| @@ -545,7 +539,8 @@ | ||
| 545 | 539 | connection.write(peersFuture); |
| 546 | 540 | |
| 547 | 541 | String partitioner = null; |
| 548 | - Map<Host, Collection<String>> tokenMap = new HashMap<Host, Collection<String>>(); | |
| 542 | + Token.Factory factory = null; | |
| 543 | + Map<Host, Set<Token>> tokenMap = new HashMap<Host, Set<Token>>(); | |
| 549 | 544 | |
| 550 | 545 | // Update cluster name, DC and rack for the one node we are connected to |
| 551 | 546 | Row localRow = localFuture.get().one(); |
| @@ -555,8 +550,10 @@ | ||
| 555 | 550 | cluster.metadata.clusterName = clusterName; |
| 556 | 551 | |
| 557 | 552 | partitioner = localRow.getString("partitioner"); |
| 558 | - if (partitioner != null) | |
| 553 | + if (partitioner != null) { | |
| 559 | 554 | cluster.metadata.partitioner = partitioner; |
| 555 | + factory = Token.getFactory(partitioner); | |
| 556 | + } | |
| 560 | 557 | |
| 561 | 558 | Host host = cluster.metadata.getHost(connection.address); |
| 562 | 559 | // In theory host can't be null. However there is no point in risking a NPE in case we |
| @@ -565,10 +562,12 @@ | ||
| 565 | 562 | logger.debug("Host in local system table ({}) unknown to us (ok if said host just got removed)", connection.address); |
| 566 | 563 | } else { |
| 567 | 564 | updateInfo(host, localRow, cluster, isInitialConnection); |
| 568 | - if (metadataEnabled) { | |
| 569 | - Set<String> tokens = localRow.getSet("tokens", String.class); | |
| 570 | - if (partitioner != null && !tokens.isEmpty()) | |
| 565 | + if (metadataEnabled && factory != null) { | |
| 566 | + Set<String> tokensStr = localRow.getSet("tokens", String.class); | |
| 567 | + if (!tokensStr.isEmpty()) { | |
| 568 | + Set<Token> tokens = toTokens(factory, tokensStr); | |
| 571 | 569 | tokenMap.put(host, tokens); |
| 570 | + } | |
| 572 | 571 | } |
| 573 | 572 | } |
| 574 | 573 | } |
| @@ -579,7 +578,7 @@ | ||
| 579 | 578 | List<String> cassandraVersions = new ArrayList<String>(); |
| 580 | 579 | List<InetAddress> broadcastAddresses = new ArrayList<InetAddress>(); |
| 581 | 580 | List<InetAddress> listenAddresses = new ArrayList<InetAddress>(); |
| 582 | - List<Set<String>> allTokens = new ArrayList<Set<String>>(); | |
| 581 | + List<Set<Token>> allTokens = new ArrayList<Set<Token>>(); | |
| 583 | 582 | List<String> dseVersions = new ArrayList<String>(); |
| 584 | 583 | List<Boolean> dseGraphEnabled = new ArrayList<Boolean>(); |
| 585 | 584 | List<String> dseWorkloads = new ArrayList<String>(); |
| @@ -596,8 +595,14 @@ | ||
| 596 | 595 | racks.add(row.getString("rack")); |
| 597 | 596 | cassandraVersions.add(row.getString("release_version")); |
| 598 | 597 | broadcastAddresses.add(row.getInet("peer")); |
| 599 | - if (metadataEnabled) | |
| 600 | - allTokens.add(row.getSet("tokens", String.class)); | |
| 598 | + if (metadataEnabled && factory != null) { | |
| 599 | + Set<String> tokensStr = row.getSet("tokens", String.class); | |
| 600 | + Set<Token> tokens = null; | |
| 601 | + if (!tokensStr.isEmpty()) { | |
| 602 | + tokens = toTokens(factory, tokensStr); | |
| 603 | + } | |
| 604 | + allTokens.add(tokens); | |
| 605 | + } | |
| 601 | 606 | InetAddress listenAddress = row.getColumnDefinitions().contains("listen_address") ? row.getInet("listen_address") : null; |
| 602 | 607 | listenAddresses.add(listenAddress); |
| 603 | 608 | String dseWorkload = row.getColumnDefinitions().contains("workload") ? row.getString("workload") : null; |
| @@ -640,7 +645,7 @@ | ||
| 640 | 645 | if (dseGraphEnabled.get(i) != null) |
| 641 | 646 | host.setDseGraphEnabled(dseGraphEnabled.get(i)); |
| 642 | 647 | |
| 643 | - if (metadataEnabled && partitioner != null && !allTokens.get(i).isEmpty()) | |
| 648 | + if (metadataEnabled && factory != null && allTokens.get(i) != null) | |
| 644 | 649 | tokenMap.put(host, allTokens.get(i)); |
| 645 | 650 | |
| 646 | 651 | if (isNew && !isInitialConnection) |
| @@ -653,8 +658,16 @@ | ||
| 653 | 658 | if (!host.getSocketAddress().equals(connection.address) && !foundHostsSet.contains(host.getSocketAddress())) |
| 654 | 659 | cluster.removeHost(host, isInitialConnection); |
| 655 | 660 | |
| 656 | - if (metadataEnabled) | |
| 657 | - cluster.metadata.rebuildTokenMap(partitioner, tokenMap); | |
| 661 | + if (metadataEnabled && factory != null && !tokenMap.isEmpty()) | |
| 662 | + cluster.metadata.rebuildTokenMap(factory, tokenMap); | |
| 663 | + } | |
| 664 | + | |
| 665 | + private static Set<Token> toTokens(Token.Factory factory, Set<String> tokensStr) { | |
| 666 | + Set<Token> tokens = new LinkedHashSet<Token>(tokensStr.size()); | |
| 667 | + for (String tokenStr : tokensStr) { | |
| 668 | + tokens.add(factory.fromString(tokenStr)); | |
| 669 | + } | |
| 670 | + return tokens; | |
| 658 | 671 | } |
| 659 | 672 | |
| 660 | 673 | private static boolean isValidPeer(Row peerRow, boolean logIfInvalid) { |
| @@ -756,13 +769,15 @@ | ||
| 756 | 769 | public void onUp(Host host) { |
| 757 | 770 | } |
| 758 | 771 | |
| 772 | + public void onAdd(Host host) { | |
| 773 | + } | |
| 774 | + | |
| 759 | 775 | public void onDown(Host host) { |
| 760 | 776 | onHostGone(host); |
| 761 | 777 | } |
| 762 | 778 | |
| 763 | 779 | public void onRemove(Host host) { |
| 764 | 780 | onHostGone(host); |
| 765 | - cluster.submitNodeListRefresh(); | |
| 766 | 781 | } |
| 767 | 782 | |
| 768 | 783 | private void onHostGone(Host host) { |
| @@ -783,11 +798,4 @@ | ||
| 783 | 798 | backgroundReconnect(0); |
| 784 | 799 | } |
| 785 | 800 | |
| 786 | - public void onAdd(Host host) { | |
| 787 | - // Refresh infos and token map if we didn't knew about that host, i.e. if we either don't have basic infos on it, | |
| 788 | - // or it's not part of our computed token map | |
| 789 | - Metadata.TokenMap tkmap = cluster.metadata.tokenMap; | |
| 790 | - if (host.getCassandraVersion() == null || tkmap == null || !tkmap.hosts.contains(host)) | |
| 791 | - cluster.submitNodeListRefresh(); | |
| 792 | - } | |
| 793 | 801 | } |
modified driver-core/src/main/java/com/datastax/driver/core/Metadata.java +47 −41
| @@ -41,7 +41,7 @@ | ||
| 41 | 41 | volatile String partitioner; |
| 42 | 42 | private final ConcurrentMap<InetSocketAddress, Host> hosts = new ConcurrentHashMap<InetSocketAddress, Host>(); |
| 43 | 43 | final ConcurrentMap<String, KeyspaceMetadata> keyspaces = new ConcurrentHashMap<String, KeyspaceMetadata>(); |
| 44 | - volatile TokenMap tokenMap; | |
| 44 | + private volatile TokenMap tokenMap; | |
| 45 | 45 | |
| 46 | 46 | final ReentrantLock lock = new ReentrantLock(); |
| 47 | 47 | |
| @@ -62,18 +62,28 @@ | ||
| 62 | 62 | this.cluster = cluster; |
| 63 | 63 | } |
| 64 | 64 | |
| 65 | - void rebuildTokenMap(String partitioner, Map<Host, Collection<String>> allTokens) { | |
| 65 | + // rebuilds the token map with the current hosts, typically when refreshing schema metadata | |
| 66 | + void rebuildTokenMap() { | |
| 66 | 67 | lock.lock(); |
| 67 | 68 | try { |
| 68 | - if (allTokens.isEmpty()) | |
| 69 | - return; | |
| 70 | - | |
| 71 | - Token.Factory factory = partitioner == null | |
| 72 | - ? (tokenMap == null ? null : tokenMap.factory) | |
| 73 | - : Token.getFactory(partitioner); | |
| 74 | - if (factory == null) | |
| 69 | + if (tokenMap == null) | |
| 75 | 70 | return; |
| 71 | + this.tokenMap = TokenMap.build( | |
| 72 | + tokenMap.factory, | |
| 73 | + tokenMap.primaryToTokens, | |
| 74 | + keyspaces.values(), | |
| 75 | + tokenMap.ring, | |
| 76 | + tokenMap.tokenRanges, | |
| 77 | + tokenMap.tokenToPrimary); | |
| 78 | + } finally { | |
| 79 | + lock.unlock(); | |
| 80 | + } | |
| 81 | + } | |
| 76 | 82 | |
| 83 | + // rebuilds the token map for a new set of hosts, typically when refreshing nodes list | |
| 84 | + void rebuildTokenMap(Token.Factory factory, Map<Host, Set<Token>> allTokens) { | |
| 85 | + lock.lock(); | |
| 86 | + try { | |
| 77 | 87 | this.tokenMap = TokenMap.build(factory, allTokens, keyspaces.values()); |
| 78 | 88 | } finally { |
| 79 | 89 | lock.unlock(); |
| @@ -264,7 +274,7 @@ | ||
| 264 | 274 | if (current == null) { |
| 265 | 275 | return Collections.emptySet(); |
| 266 | 276 | } else { |
| 267 | - Map<Host, Set<TokenRange>> dcRanges = current.hostsToRanges.get(keyspace); | |
| 277 | + Map<Host, Set<TokenRange>> dcRanges = current.hostsToRangesByKeyspace.get(keyspace); | |
| 268 | 278 | if (dcRanges == null) { |
| 269 | 279 | return Collections.emptySet(); |
| 270 | 280 | } else { |
| @@ -384,7 +394,7 @@ | ||
| 384 | 394 | KeyspaceMetadata removeKeyspace(String keyspace) { |
| 385 | 395 | KeyspaceMetadata removed = keyspaces.remove(keyspace); |
| 386 | 396 | if (tokenMap != null) |
| 387 | - tokenMap.tokenToHosts.remove(keyspace); | |
| 397 | + tokenMap.tokenToHostsByKeyspace.remove(keyspace); | |
| 388 | 398 | return removed; |
| 389 | 399 | } |
| 390 | 400 | |
| @@ -609,61 +619,57 @@ | ||
| 609 | 619 | } |
| 610 | 620 | } |
| 611 | 621 | |
| 612 | - static class TokenMap { | |
| 622 | + private static class TokenMap { | |
| 613 | 623 | |
| 614 | 624 | private final Token.Factory factory; |
| 615 | - private final Map<String, Map<Token, Set<Host>>> tokenToHosts; | |
| 616 | - private final Map<String, Map<Host, Set<TokenRange>>> hostsToRanges; | |
| 625 | + private final Map<Host, Set<Token>> primaryToTokens; | |
| 626 | + private final Map<String, Map<Token, Set<Host>>> tokenToHostsByKeyspace; | |
| 627 | + private final Map<String, Map<Host, Set<TokenRange>>> hostsToRangesByKeyspace; | |
| 617 | 628 | private final List<Token> ring; |
| 618 | 629 | private final Set<TokenRange> tokenRanges; |
| 619 | - final Set<Host> hosts; | |
| 630 | + private final Map<Token, Host> tokenToPrimary; | |
| 620 | 631 | |
| 621 | 632 | private TokenMap(Token.Factory factory, |
| 633 | + List<Token> ring, | |
| 634 | + Set<TokenRange> tokenRanges, | |
| 635 | + Map<Token, Host> tokenToPrimary, | |
| 622 | 636 | Map<Host, Set<Token>> primaryToTokens, |
| 623 | - Map<String, Map<Token, Set<Host>>> tokenToHosts, | |
| 624 | - Map<String, Map<Host, Set<TokenRange>>> hostsToRanges, | |
| 625 | - List<Token> ring, Set<TokenRange> tokenRanges, Set<Host> hosts) { | |
| 637 | + Map<String, Map<Token, Set<Host>>> tokenToHostsByKeyspace, | |
| 638 | + Map<String, Map<Host, Set<TokenRange>>> hostsToRangesByKeyspace) { | |
| 626 | 639 | this.factory = factory; |
| 627 | - this.tokenToHosts = tokenToHosts; | |
| 628 | - this.hostsToRanges = hostsToRanges; | |
| 629 | 640 | this.ring = ring; |
| 630 | 641 | this.tokenRanges = tokenRanges; |
| 631 | - this.hosts = hosts; | |
| 642 | + this.tokenToPrimary = tokenToPrimary; | |
| 643 | + this.primaryToTokens = primaryToTokens; | |
| 644 | + this.tokenToHostsByKeyspace = tokenToHostsByKeyspace; | |
| 645 | + this.hostsToRangesByKeyspace = hostsToRangesByKeyspace; | |
| 632 | 646 | for (Map.Entry<Host, Set<Token>> entry : primaryToTokens.entrySet()) { |
| 633 | 647 | Host host = entry.getKey(); |
| 634 | 648 | host.setTokens(ImmutableSet.copyOf(entry.getValue())); |
| 635 | 649 | } |
| 636 | 650 | } |
| 637 | 651 | |
| 638 | - public static TokenMap build(Token.Factory factory, Map<Host, Collection<String>> allTokens, Collection<KeyspaceMetadata> keyspaces) { | |
| 639 | - | |
| 640 | - Set<Host> hosts = allTokens.keySet(); | |
| 652 | + private static TokenMap build(Token.Factory factory, Map<Host, Set<Token>> allTokens, Collection<KeyspaceMetadata> keyspaces) { | |
| 641 | 653 | Map<Token, Host> tokenToPrimary = new HashMap<Token, Host>(); |
| 642 | - Map<Host, Set<Token>> primaryToTokens = new HashMap<Host, Set<Token>>(); | |
| 643 | 654 | Set<Token> allSorted = new TreeSet<Token>(); |
| 644 | - | |
| 645 | - for (Map.Entry<Host, Collection<String>> entry : allTokens.entrySet()) { | |
| 655 | + for (Map.Entry<Host, ? extends Collection<Token>> entry : allTokens.entrySet()) { | |
| 646 | 656 | Host host = entry.getKey(); |
| 647 | - for (String tokenStr : entry.getValue()) { | |
| 657 | + for (Token t : entry.getValue()) { | |
| 648 | 658 | try { |
| 649 | - Token t = factory.fromString(tokenStr); | |
| 650 | 659 | allSorted.add(t); |
| 651 | 660 | tokenToPrimary.put(t, host); |
| 652 | - Set<Token> hostTokens = primaryToTokens.get(host); | |
| 653 | - if (hostTokens == null) { | |
| 654 | - hostTokens = new HashSet<Token>(); | |
| 655 | - primaryToTokens.put(host, hostTokens); | |
| 656 | - } | |
| 657 | - hostTokens.add(t); | |
| 658 | 661 | } catch (IllegalArgumentException e) { |
| 659 | 662 | // If we failed parsing that token, skip it |
| 660 | 663 | } |
| 661 | 664 | } |
| 662 | 665 | } |
| 663 | - | |
| 664 | 666 | List<Token> ring = new ArrayList<Token>(allSorted); |
| 665 | 667 | Set<TokenRange> tokenRanges = makeTokenRanges(ring, factory); |
| 668 | + return build(factory, allTokens, keyspaces, ring, tokenRanges, tokenToPrimary); | |
| 669 | + } | |
| 666 | 670 | |
| 671 | + private static TokenMap build(Token.Factory factory, Map<Host, Set<Token>> allTokens, Collection<KeyspaceMetadata> keyspaces, List<Token> ring, Set<TokenRange> tokenRanges, Map<Token, Host> tokenToPrimary) { | |
| 672 | + Set<Host> hosts = allTokens.keySet(); | |
| 667 | 673 | Map<String, Map<Token, Set<Host>>> tokenToHosts = new HashMap<String, Map<Token, Set<Host>>>(); |
| 668 | 674 | Map<ReplicationStrategy, Map<Token, Set<Host>>> replStrategyToHosts = new HashMap<ReplicationStrategy, Map<Token, Set<Host>>>(); |
| 669 | 675 | Map<String, Map<Host, Set<TokenRange>>> hostsToRanges = new HashMap<String, Map<Host, Set<TokenRange>>>(); |
| @@ -691,17 +697,17 @@ | ||
| 691 | 697 | } |
| 692 | 698 | hostsToRanges.put(keyspace.getName(), ksRanges); |
| 693 | 699 | } |
| 694 | - return new TokenMap(factory, primaryToTokens, tokenToHosts, hostsToRanges, ring, tokenRanges, hosts); | |
| 700 | + return new TokenMap(factory, ring, tokenRanges, tokenToPrimary, allTokens, tokenToHosts, hostsToRanges); | |
| 695 | 701 | } |
| 696 | 702 | |
| 697 | 703 | private Set<Host> getReplicas(String keyspace, Token token) { |
| 698 | 704 | |
| 699 | - Map<Token, Set<Host>> keyspaceHosts = tokenToHosts.get(keyspace); | |
| 700 | - if (keyspaceHosts == null) | |
| 705 | + Map<Token, Set<Host>> tokenToHosts = tokenToHostsByKeyspace.get(keyspace); | |
| 706 | + if (tokenToHosts == null) | |
| 701 | 707 | return Collections.emptySet(); |
| 702 | 708 | |
| 703 | 709 | // If the token happens to be one of the "primary" tokens, get result directly |
| 704 | - Set<Host> hosts = keyspaceHosts.get(token); | |
| 710 | + Set<Host> hosts = tokenToHosts.get(token); | |
| 705 | 711 | if (hosts != null) |
| 706 | 712 | return hosts; |
| 707 | 713 | |
| @@ -713,7 +719,7 @@ | ||
| 713 | 719 | i = 0; |
| 714 | 720 | } |
| 715 | 721 | |
| 716 | - return keyspaceHosts.get(ring.get(i)); | |
| 722 | + return tokenToHosts.get(ring.get(i)); | |
| 717 | 723 | } |
| 718 | 724 | |
| 719 | 725 | private static Map<Token, Set<Host>> makeNonReplicatedMap(Map<Token, Host> input) { |
modified driver-core/src/main/java/com/datastax/driver/core/QueryOptions.java +15 −1
| @@ -16,6 +16,8 @@ | ||
| 16 | 16 | package com.datastax.driver.core; |
| 17 | 17 | |
| 18 | 18 | import com.datastax.driver.core.exceptions.UnsupportedFeatureException; |
| 19 | +import com.datastax.driver.core.utils.MoreFutures; | |
| 20 | +import com.google.common.util.concurrent.Futures; | |
| 19 | 21 | |
| 20 | 22 | /** |
| 21 | 23 | * Options related to defaults for individual queries. |
| @@ -299,7 +301,19 @@ | ||
| 299 | 301 | boolean wasEnabled = this.metadataEnabled; |
| 300 | 302 | this.metadataEnabled = enabled; |
| 301 | 303 | if (!wasEnabled && enabled && manager != null) { |
| 302 | - manager.submitSchemaRefresh(null, null, null, null); // will also refresh token map | |
| 304 | + // This is roughly the same as what we do in ControlConnection.tryConnect(): | |
| 305 | + // 1. call submitNodeListRefresh() first to | |
| 306 | + // be able to compute the token map for the first time, | |
| 307 | + // which will be incomplete due to the lack of keyspace metadata | |
| 308 | + Futures.addCallback(manager.submitNodeListRefresh(), new MoreFutures.SuccessCallback<Void>() { | |
| 309 | + @Override | |
| 310 | + public void onSuccess(Void result) { | |
| 311 | + // 2. then call submitSchemaRefresh() to | |
| 312 | + // refresh schema metadata and re-compute the token map | |
| 313 | + // this time with information about keyspaces | |
| 314 | + manager.submitSchemaRefresh(null, null, null, null); | |
| 315 | + } | |
| 316 | + }); | |
| 303 | 317 | } |
| 304 | 318 | return this; |
| 305 | 319 | } |
modified driver-core/src/main/java/com/datastax/driver/core/SchemaParser.java +3 −0
| @@ -63,6 +63,9 @@ | ||
| 63 | 63 | assert rows.keyspaces != null; |
| 64 | 64 | Map<String, KeyspaceMetadata> keyspaces = buildKeyspaces(rows, cassandraVersion, cluster); |
| 65 | 65 | updateKeyspaces(metadata, metadata.keyspaces, keyspaces, targetKeyspace); |
| 66 | + // If we rebuild all from scratch or have an updated keyspace, rebuild the token map | |
| 67 | + // since some replication on some keyspace may have changed | |
| 68 | + metadata.rebuildTokenMap(); | |
| 66 | 69 | } else { |
| 67 | 70 | assert targetKeyspace != null; |
| 68 | 71 | KeyspaceMetadata keyspace = metadata.keyspaces.get(targetKeyspace); |
modified driver-core/src/test/java/com/datastax/driver/core/NodeListRefreshDebouncerTest.java +7 −39
| @@ -18,11 +18,7 @@ | ||
| 18 | 18 | import org.testng.annotations.BeforeMethod; |
| 19 | 19 | import org.testng.annotations.Test; |
| 20 | 20 | |
| 21 | -import java.util.Collection; | |
| 22 | - | |
| 23 | 21 | import static com.datastax.driver.core.CreateCCM.TestMode.PER_METHOD; |
| 24 | -import static com.datastax.driver.core.TestUtils.CREATE_KEYSPACE_SIMPLE_FORMAT; | |
| 25 | -import static com.google.common.collect.Lists.newArrayList; | |
| 26 | 22 | import static org.mockito.Mockito.*; |
| 27 | 23 | |
| 28 | 24 | @CreateCCM(PER_METHOD) |
| @@ -31,21 +27,14 @@ | ||
| 31 | 27 | |
| 32 | 28 | private static final int DEBOUNCE_TIME = 2000; |
| 33 | 29 | |
| 34 | - QueryOptions queryOptions; | |
| 35 | - | |
| 36 | - Cluster cluster2; | |
| 37 | - | |
| 38 | - Session session2; | |
| 30 | + private Cluster cluster2; | |
| 39 | 31 | |
| 40 | 32 | // Control Connection to be spied. |
| 41 | 33 | private ControlConnection controlConnection; |
| 42 | 34 | |
| 43 | - // Keyspaces to drop after test completes. | |
| 44 | - private Collection<String> keyspaces = newArrayList(); | |
| 45 | - | |
| 46 | 35 | @BeforeMethod(groups = "short") |
| 47 | 36 | public void setup() { |
| 48 | - queryOptions = new QueryOptions(); | |
| 37 | + QueryOptions queryOptions = new QueryOptions(); | |
| 49 | 38 | queryOptions.setRefreshNodeListIntervalMillis(DEBOUNCE_TIME); |
| 50 | 39 | queryOptions.setMaxPendingRefreshNodeListRequests(5); |
| 51 | 40 | queryOptions.setRefreshSchemaIntervalMillis(0); |
| @@ -55,7 +44,8 @@ | ||
| 55 | 44 | .withPort(ccm().getBinaryPort()) |
| 56 | 45 | .withQueryOptions(queryOptions) |
| 57 | 46 | .build()); |
| 58 | - session2 = cluster2.connect(); | |
| 47 | + | |
| 48 | + cluster2.init(); | |
| 59 | 49 | |
| 60 | 50 | // Create a spy of the Cluster's control connection and replace it with the spy. |
| 61 | 51 | controlConnection = spy(cluster2.manager.controlConnection); |
| @@ -63,23 +53,6 @@ | ||
| 63 | 53 | reset(controlConnection); |
| 64 | 54 | } |
| 65 | 55 | |
| 66 | - /** | |
| 67 | - * Ensures that when a keyspace is created that refresh node list request | |
| 68 | - * is debounced and processed within {@link QueryOptions#getRefreshSchemaIntervalMillis()} | |
| 69 | - * + {@link QueryOptions#getRefreshNodeListIntervalMillis()}. | |
| 70 | - * | |
| 71 | - * @jira_ticket JAVA-657 | |
| 72 | - * @since 2.0.11 | |
| 73 | - */ | |
| 74 | - @Test(groups = "short") | |
| 75 | - public void should_debounce_refresh_when_keyspace_created() { | |
| 76 | - String keyspace = "sdrwkc"; | |
| 77 | - session().execute(String.format(CREATE_KEYSPACE_SIMPLE_FORMAT, keyspace, 1)); | |
| 78 | - keyspaces.add(keyspace); | |
| 79 | - | |
| 80 | - verify(controlConnection, timeout(DEBOUNCE_TIME + queryOptions.getRefreshSchemaIntervalMillis())).refreshNodeListAndTokenMap(); | |
| 81 | - } | |
| 82 | - | |
| 83 | 56 | /** |
| 84 | 57 | * Ensures that when enough refreshNodeList requests have been received |
| 85 | 58 | * to reach {@link QueryOptions#getMaxPendingRefreshNodeListRequests()} that a |
| @@ -90,16 +63,11 @@ | ||
| 90 | 63 | */ |
| 91 | 64 | @Test(groups = "short") |
| 92 | 65 | public void should_refresh_when_max_pending_requests_reached() { |
| 93 | - // Create keyspaces 5 times to cause | |
| 94 | - // refreshNodeListAndTokenMap to be called. | |
| 95 | - String prefix = "srwmprr"; | |
| 96 | 66 | for (int i = 0; i < 5; i++) { |
| 97 | - String keyspace = prefix + i; | |
| 98 | - session().execute(String.format(CREATE_KEYSPACE_SIMPLE_FORMAT, keyspace, 1)); | |
| 99 | - keyspaces.add(keyspace); | |
| 67 | + cluster2.manager.submitNodeListRefresh(); | |
| 100 | 68 | } |
| 101 | 69 | |
| 102 | - // add a 1 second delay to account for executor submit. | |
| 103 | - verify(controlConnection, timeout(1000)).refreshNodeListAndTokenMap(); | |
| 70 | + // add delay to account for executor submit. | |
| 71 | + verify(controlConnection, timeout(DEBOUNCE_TIME)).refreshNodeListAndTokenMap(); | |
| 104 | 72 | } |
| 105 | 73 | } |
modified driver-core/src/test/java/com/datastax/driver/core/SchemaChangesTest.java +59 −67
| @@ -44,7 +44,9 @@ | ||
| 44 | 44 | private static final String DROP_TABLE = "DROP TABLE %s.table1"; |
| 45 | 45 | |
| 46 | 46 | /** |
| 47 | - * The maximum time that the test will wait to check that listeners have been notified | |
| 47 | + * The maximum time that the test will wait to check that listeners have been notified. | |
| 48 | + * This threshold is intentionally set to a very high value to allow CI tests | |
| 49 | + * to pass. | |
| 48 | 50 | */ |
| 49 | 51 | private static final long NOTIF_TIMEOUT_MS = TimeUnit.MINUTES.toMillis(1); |
| 50 | 52 | |
| @@ -89,7 +91,7 @@ | ||
| 89 | 91 | cluster2.init(); |
| 90 | 92 | |
| 91 | 93 | cluster1.register(listener1 = mock(SchemaChangeListener.class)); |
| 92 | - cluster1.register(listener2 = mock(SchemaChangeListener.class)); | |
| 94 | + cluster2.register(listener2 = mock(SchemaChangeListener.class)); | |
| 93 | 95 | listeners = Lists.newArrayList(listener1, listener2); |
| 94 | 96 | |
| 95 | 97 | schemaDisabledCluster.register(schemaDisabledListener = mock(SchemaChangeListener.class)); |
| @@ -150,7 +152,6 @@ | ||
| 150 | 152 | .isInKeyspace(handleId(keyspace)) |
| 151 | 153 | .hasName("table1"); |
| 152 | 154 | } |
| 153 | - refreshSchema(); | |
| 154 | 155 | for (Metadata m : metadatas()) |
| 155 | 156 | assertThat(m.getKeyspace(keyspace).getTable("table1")).isNotNull(); |
| 156 | 157 | } |
| @@ -166,7 +167,6 @@ | ||
| 166 | 167 | .isInKeyspace(handleId(keyspace)) |
| 167 | 168 | .hasName("table1"); |
| 168 | 169 | } |
| 169 | - refreshSchema(); | |
| 170 | 170 | for (Metadata m : metadatas()) |
| 171 | 171 | assertThat(m.getKeyspace(keyspace).getTable("table1")).hasNoColumn("j"); |
| 172 | 172 | assert added != null; |
| @@ -183,7 +183,6 @@ | ||
| 183 | 183 | .hasName("table1") |
| 184 | 184 | .hasColumn("j"); |
| 185 | 185 | } |
| 186 | - refreshSchema(); | |
| 187 | 186 | for (Metadata m : metadatas()) |
| 188 | 187 | assertThat(m.getKeyspace(keyspace).getTable("table1")).hasColumn("j"); |
| 189 | 188 | } |
| @@ -198,14 +197,12 @@ | ||
| 198 | 197 | .hasName("table1") |
| 199 | 198 | .isInKeyspace(handleId(keyspace)); |
| 200 | 199 | } |
| 201 | - refreshSchema(); | |
| 202 | 200 | execute(DROP_TABLE, keyspace); |
| 203 | 201 | for (SchemaChangeListener listener : listeners) { |
| 204 | 202 | ArgumentCaptor<TableMetadata> removed = ArgumentCaptor.forClass(TableMetadata.class); |
| 205 | 203 | verify(listener, timeout(NOTIF_TIMEOUT_MS).times(1)).onTableRemoved(removed.capture()); |
| 206 | 204 | assertThat(removed.getValue()).hasName("table1"); |
| 207 | 205 | } |
| 208 | - refreshSchema(); | |
| 209 | 206 | for (Metadata m : metadatas()) |
| 210 | 207 | assertThat(m.getKeyspace(keyspace).getTable("table1")).isNull(); |
| 211 | 208 | } |
| @@ -221,7 +218,6 @@ | ||
| 221 | 218 | assertThat((DataType) added.getValue()) |
| 222 | 219 | .isUserType(handleId(keyspace), "type1"); |
| 223 | 220 | } |
| 224 | - refreshSchema(); | |
| 225 | 221 | for (Metadata m : metadatas()) |
| 226 | 222 | assertThat((DataType) m.getKeyspace(keyspace).getUserType("type1")).isNotNull(); |
| 227 | 223 | } |
| @@ -238,7 +234,6 @@ | ||
| 238 | 234 | assertThat(previous.getValue().getFieldNames()).doesNotContain("j"); |
| 239 | 235 | assertThat(current.getValue().getFieldNames()).contains("j"); |
| 240 | 236 | } |
| 241 | - refreshSchema(); | |
| 242 | 237 | for (Metadata m : metadatas()) |
| 243 | 238 | assertThat(m.getKeyspace(keyspace).getUserType("type1").getFieldType("j")).isNotNull(); |
| 244 | 239 | } |
| @@ -254,7 +249,6 @@ | ||
| 254 | 249 | verify(listener, timeout(NOTIF_TIMEOUT_MS).times(1)).onUserTypeRemoved(removed.capture()); |
| 255 | 250 | assertThat((DataType) removed.getValue()).isUserType(handleId(keyspace), "type1"); |
| 256 | 251 | } |
| 257 | - refreshSchema(); | |
| 258 | 252 | for (Metadata m : metadatas()) |
| 259 | 253 | assertThat((DataType) m.getKeyspace(keyspace).getUserType("type1")).isNull(); |
| 260 | 254 | } |
| @@ -263,7 +257,6 @@ | ||
| 263 | 257 | @CassandraVersion(major = 2.2) |
| 264 | 258 | public void should_notify_of_function_creation(String keyspace) { |
| 265 | 259 | session1.execute(String.format("CREATE FUNCTION %s.\"ID\"(i int) RETURNS NULL ON NULL INPUT RETURNS int LANGUAGE java AS 'return i;'", keyspace)); |
| 266 | - | |
| 267 | 260 | for (SchemaChangeListener listener : listeners) { |
| 268 | 261 | ArgumentCaptor<FunctionMetadata> added = ArgumentCaptor.forClass(FunctionMetadata.class); |
| 269 | 262 | verify(listener, timeout(NOTIF_TIMEOUT_MS).times(1)).onFunctionAdded(added.capture()); |
| @@ -271,7 +264,6 @@ | ||
| 271 | 264 | .isInKeyspace(handleId(keyspace)) |
| 272 | 265 | .hasSignature("\"ID\"(int)"); |
| 273 | 266 | } |
| 274 | - refreshSchema(); | |
| 275 | 267 | for (Metadata m : metadatas()) |
| 276 | 268 | assertThat(m.getKeyspace(keyspace).getFunction("\"ID\"", DataType.cint())) |
| 277 | 269 | .isNotNull(); |
| @@ -280,47 +272,44 @@ | ||
| 280 | 272 | @Test(groups = "short", dataProvider = "existingKeyspaceName") |
| 281 | 273 | @CassandraVersion(major = 2.2) |
| 282 | 274 | public void should_notify_of_function_update(String keyspace) { |
| 283 | - session1.execute(String.format("CREATE TYPE IF NOT EXISTS %s.user (\"ID\" int, name text)", keyspace)); | |
| 284 | - session1.execute(String.format("CREATE FUNCTION %s.\"ID\"(user user) RETURNS NULL ON NULL INPUT RETURNS int LANGUAGE java AS 'return user.getInt(\"ID\");'", keyspace)); | |
| 285 | - refreshSchema(); | |
| 286 | - | |
| 275 | + session1.execute(String.format("CREATE FUNCTION %s.\"ID\"(i int) RETURNS NULL ON NULL INPUT RETURNS int LANGUAGE java AS 'return i;'", keyspace)); | |
| 276 | + for (SchemaChangeListener listener : listeners) { | |
| 277 | + ArgumentCaptor<FunctionMetadata> added = ArgumentCaptor.forClass(FunctionMetadata.class); | |
| 278 | + verify(listener, timeout(NOTIF_TIMEOUT_MS).times(1)).onFunctionAdded(added.capture()); | |
| 279 | + assertThat(added.getValue()) | |
| 280 | + .isInKeyspace(handleId(keyspace)) | |
| 281 | + .hasSignature("\"ID\"(int)"); | |
| 282 | + } | |
| 287 | 283 | for (Metadata m : metadatas()) |
| 288 | - assertThat(m.getKeyspace(keyspace).getFunction("\"ID\"", m.getKeyspace(keyspace).getUserType("user")).getBody()) | |
| 289 | - .isEqualTo("return user.getInt(\"ID\");"); | |
| 290 | - | |
| 291 | - session1.execute(String.format("CREATE OR REPLACE FUNCTION %s.\"ID\"(user user) RETURNS NULL ON NULL INPUT RETURNS int LANGUAGE java " | |
| 292 | - + "AS 'return 1 + user.getInt(\"ID\");'", keyspace)); | |
| 293 | - | |
| 284 | + assertThat(m.getKeyspace(keyspace).getFunction("\"ID\"", DataType.cint())) | |
| 285 | + .isNotNull(); | |
| 286 | + session1.execute(String.format("CREATE OR REPLACE FUNCTION %s.\"ID\"(i int) RETURNS NULL ON NULL INPUT RETURNS int LANGUAGE java AS 'return i + 1;'", keyspace)); | |
| 294 | 287 | for (SchemaChangeListener listener : listeners) { |
| 295 | 288 | ArgumentCaptor<FunctionMetadata> current = ArgumentCaptor.forClass(FunctionMetadata.class); |
| 296 | 289 | ArgumentCaptor<FunctionMetadata> previous = ArgumentCaptor.forClass(FunctionMetadata.class); |
| 297 | 290 | verify(listener, timeout(NOTIF_TIMEOUT_MS).times(1)).onFunctionChanged(current.capture(), previous.capture()); |
| 298 | - assertThat(previous.getValue()).hasBody("return user.getInt(\"ID\");"); | |
| 299 | - assertThat(current.getValue()).hasBody("return 1 + user.getInt(\"ID\");"); | |
| 291 | + assertThat(previous.getValue()).hasBody("return i;"); | |
| 292 | + assertThat(current.getValue()).hasBody("return i + 1;"); | |
| 300 | 293 | } |
| 301 | - refreshSchema(); | |
| 302 | 294 | for (Metadata m : metadatas()) |
| 303 | - assertThat(m.getKeyspace(keyspace).getFunction("\"ID\"", m.getKeyspace(keyspace).getUserType("user")).getBody()) | |
| 304 | - .isEqualTo("return 1 + user.getInt(\"ID\");"); | |
| 295 | + assertThat(m.getKeyspace(keyspace).getFunction("\"ID\"", DataType.cint()).getBody()) | |
| 296 | + .isEqualTo("return i + 1;"); | |
| 305 | 297 | } |
| 306 | 298 | |
| 307 | 299 | @Test(groups = "short", dataProvider = "existingKeyspaceName") |
| 308 | 300 | @CassandraVersion(major = 2.2) |
| 309 | 301 | public void should_notify_of_function_drop(String keyspace) { |
| 310 | - session1.execute(String.format("CREATE TYPE IF NOT EXISTS %s.user (\"ID\" int, name text)", keyspace)); | |
| 311 | - session1.execute(String.format("CREATE FUNCTION %s.\"ID\"(user user) RETURNS NULL ON NULL INPUT RETURNS int LANGUAGE java AS 'return user.getInt(\"ID\");'", keyspace)); | |
| 302 | + session1.execute(String.format("CREATE FUNCTION %s.\"ID\"(i int) RETURNS NULL ON NULL INPUT RETURNS int LANGUAGE java AS 'return i;'", keyspace)); | |
| 312 | 303 | session1.execute(String.format("DROP FUNCTION %s.\"ID\"", keyspace)); |
| 313 | - | |
| 314 | 304 | for (SchemaChangeListener listener : listeners) { |
| 315 | 305 | ArgumentCaptor<FunctionMetadata> removed = ArgumentCaptor.forClass(FunctionMetadata.class); |
| 316 | 306 | verify(listener, timeout(NOTIF_TIMEOUT_MS).times(1)).onFunctionRemoved(removed.capture()); |
| 317 | 307 | assertThat(removed.getValue()) |
| 318 | 308 | .isInKeyspace(handleId(keyspace)) |
| 319 | - .hasSignature("\"ID\"(user)"); | |
| 309 | + .hasSignature("\"ID\"(int)"); | |
| 320 | 310 | } |
| 321 | - refreshSchema(); | |
| 322 | 311 | for (Metadata m : metadatas()) |
| 323 | - assertThat(m.getKeyspace(keyspace).getFunction("\"ID\"", m.getKeyspace(keyspace).getUserType("user"))) | |
| 312 | + assertThat(m.getKeyspace(keyspace).getFunction("\"ID\"", DataType.cint())) | |
| 324 | 313 | .isNull(); |
| 325 | 314 | } |
| 326 | 315 | |
| @@ -330,7 +319,6 @@ | ||
| 330 | 319 | session1.execute(String.format("CREATE FUNCTION %s.\"PLUS\"(s int, v int) RETURNS NULL ON NULL INPUT RETURNS int LANGUAGE java" |
| 331 | 320 | + " AS 'return s+v;'", keyspace)); |
| 332 | 321 | session1.execute(String.format("CREATE AGGREGATE %s.\"SUM\"(int) SFUNC \"PLUS\" STYPE int INITCOND 0;", keyspace)); |
| 333 | - | |
| 334 | 322 | for (SchemaChangeListener listener : listeners) { |
| 335 | 323 | ArgumentCaptor<AggregateMetadata> added = ArgumentCaptor.forClass(AggregateMetadata.class); |
| 336 | 324 | verify(listener, timeout(NOTIF_TIMEOUT_MS).times(1)).onAggregateAdded(added.capture()); |
| @@ -338,7 +326,6 @@ | ||
| 338 | 326 | .isInKeyspace(handleId(keyspace)) |
| 339 | 327 | .hasSignature("\"SUM\"(int)"); |
| 340 | 328 | } |
| 341 | - refreshSchema(); | |
| 342 | 329 | for (Metadata m : metadatas()) |
| 343 | 330 | assertThat(m.getKeyspace(keyspace).getAggregate("\"SUM\"", DataType.cint())) |
| 344 | 331 | .isNotNull(); |
| @@ -350,13 +337,17 @@ | ||
| 350 | 337 | session1.execute(String.format("CREATE FUNCTION %s.\"PLUS\"(s int, v int) RETURNS NULL ON NULL INPUT RETURNS int LANGUAGE java" |
| 351 | 338 | + " AS 'return s+v;'", keyspace)); |
| 352 | 339 | session1.execute(String.format("CREATE AGGREGATE %s.\"SUM\"(int) SFUNC \"PLUS\" STYPE int INITCOND 0", keyspace)); |
| 353 | - refreshSchema(); | |
| 340 | + for (SchemaChangeListener listener : listeners) { | |
| 341 | + ArgumentCaptor<AggregateMetadata> added = ArgumentCaptor.forClass(AggregateMetadata.class); | |
| 342 | + verify(listener, timeout(NOTIF_TIMEOUT_MS).times(1)).onAggregateAdded(added.capture()); | |
| 343 | + assertThat(added.getValue()) | |
| 344 | + .isInKeyspace(handleId(keyspace)) | |
| 345 | + .hasSignature("\"SUM\"(int)"); | |
| 346 | + } | |
| 354 | 347 | for (Metadata m : metadatas()) |
| 355 | 348 | assertThat(m.getKeyspace(keyspace).getAggregate("\"SUM\"", DataType.cint()).getInitCond()) |
| 356 | 349 | .isEqualTo(0); |
| 357 | - | |
| 358 | 350 | session1.execute(String.format("CREATE OR REPLACE AGGREGATE %s.\"SUM\"(int) SFUNC \"PLUS\" STYPE int INITCOND 1", keyspace)); |
| 359 | - | |
| 360 | 351 | for (SchemaChangeListener listener : listeners) { |
| 361 | 352 | ArgumentCaptor<AggregateMetadata> current = ArgumentCaptor.forClass(AggregateMetadata.class); |
| 362 | 353 | ArgumentCaptor<AggregateMetadata> previous = ArgumentCaptor.forClass(AggregateMetadata.class); |
| @@ -364,7 +355,6 @@ | ||
| 364 | 355 | assertThat(previous.getValue()).hasInitCond(0); |
| 365 | 356 | assertThat(current.getValue()).hasInitCond(1); |
| 366 | 357 | } |
| 367 | - refreshSchema(); | |
| 368 | 358 | for (Metadata m : metadatas()) |
| 369 | 359 | assertThat(m.getKeyspace(keyspace).getAggregate("\"SUM\"", DataType.cint()).getInitCond()) |
| 370 | 360 | .isEqualTo(1); |
| @@ -377,7 +367,6 @@ | ||
| 377 | 367 | + " AS 'return s+v;'", keyspace)); |
| 378 | 368 | session1.execute(String.format("CREATE AGGREGATE %s.\"SUM\"(int) SFUNC \"PLUS\" STYPE int INITCOND 0", keyspace)); |
| 379 | 369 | session1.execute(String.format("DROP AGGREGATE %s.\"SUM\"", keyspace)); |
| 380 | - | |
| 381 | 370 | for (SchemaChangeListener listener : listeners) { |
| 382 | 371 | ArgumentCaptor<AggregateMetadata> removed = ArgumentCaptor.forClass(AggregateMetadata.class); |
| 383 | 372 | verify(listener, timeout(NOTIF_TIMEOUT_MS).times(1)).onAggregateRemoved(removed.capture()); |
| @@ -385,8 +374,6 @@ | ||
| 385 | 374 | .isInKeyspace(handleId(keyspace)) |
| 386 | 375 | .hasSignature("\"SUM\"(int)"); |
| 387 | 376 | } |
| 388 | - | |
| 389 | - refreshSchema(); | |
| 390 | 377 | for (Metadata m : metadatas()) |
| 391 | 378 | assertThat(m.getKeyspace(keyspace).getAggregate("\"SUM\"", DataType.cint())) |
| 392 | 379 | .isNull(); |
| @@ -397,7 +384,12 @@ | ||
| 397 | 384 | public void should_notify_of_view_creation(String keyspace) { |
| 398 | 385 | session1.execute(String.format("CREATE TABLE %s.table1 (pk int PRIMARY KEY, c int)", keyspace)); |
| 399 | 386 | session1.execute(String.format("CREATE MATERIALIZED VIEW %s.mv1 AS SELECT c FROM %s.table1 WHERE c IS NOT NULL PRIMARY KEY (pk, c)", keyspace, keyspace)); |
| 400 | - refreshSchema(); | |
| 387 | + for (SchemaChangeListener listener : listeners) { | |
| 388 | + ArgumentCaptor<MaterializedViewMetadata> removed = ArgumentCaptor.forClass(MaterializedViewMetadata.class); | |
| 389 | + verify(listener, timeout(NOTIF_TIMEOUT_MS).times(1)).onMaterializedViewAdded(removed.capture()); | |
| 390 | + assertThat(removed.getValue()) | |
| 391 | + .hasName("mv1"); | |
| 392 | + } | |
| 401 | 393 | for (Metadata m : metadatas()) |
| 402 | 394 | assertThat(m.getKeyspace(keyspace).getMaterializedView("mv1")).isNotNull(); |
| 403 | 395 | } |
| @@ -407,12 +399,26 @@ | ||
| 407 | 399 | public void should_notify_of_view_update(String keyspace) { |
| 408 | 400 | session1.execute(String.format("CREATE TABLE %s.table1 (pk int PRIMARY KEY, c int)", keyspace)); |
| 409 | 401 | session1.execute(String.format("CREATE MATERIALIZED VIEW %s.mv1 AS SELECT c FROM %s.table1 WHERE c IS NOT NULL PRIMARY KEY (pk, c) WITH compaction = { 'class' : 'SizeTieredCompactionStrategy' }", keyspace, keyspace)); |
| 410 | - refreshSchema(); | |
| 402 | + for (SchemaChangeListener listener : listeners) { | |
| 403 | + ArgumentCaptor<MaterializedViewMetadata> removed = ArgumentCaptor.forClass(MaterializedViewMetadata.class); | |
| 404 | + verify(listener, timeout(NOTIF_TIMEOUT_MS).times(1)).onMaterializedViewAdded(removed.capture()); | |
| 405 | + assertThat(removed.getValue()) | |
| 406 | + .hasName("mv1"); | |
| 407 | + assertThat(removed.getValue().getOptions().getCompaction().get("class")) | |
| 408 | + .contains("SizeTieredCompactionStrategy"); | |
| 409 | + } | |
| 411 | 410 | for (Metadata m : metadatas()) |
| 412 | 411 | assertThat(m.getKeyspace(keyspace).getMaterializedView("mv1").getOptions().getCompaction().get("class")).contains("SizeTieredCompactionStrategy"); |
| 413 | - | |
| 414 | 412 | session1.execute(String.format("ALTER MATERIALIZED VIEW %s.mv1 WITH compaction = { 'class' : 'LeveledCompactionStrategy' }", keyspace)); |
| 415 | - refreshSchema(); | |
| 413 | + for (SchemaChangeListener listener : listeners) { | |
| 414 | + ArgumentCaptor<MaterializedViewMetadata> current = ArgumentCaptor.forClass(MaterializedViewMetadata.class); | |
| 415 | + ArgumentCaptor<MaterializedViewMetadata> previous = ArgumentCaptor.forClass(MaterializedViewMetadata.class); | |
| 416 | + verify(listener, timeout(NOTIF_TIMEOUT_MS).times(1)).onMaterializedViewChanged(current.capture(), previous.capture()); | |
| 417 | + assertThat(previous.getValue().getOptions().getCompaction().get("class")) | |
| 418 | + .contains("SizeTieredCompactionStrategy"); | |
| 419 | + assertThat(current.getValue().getOptions().getCompaction().get("class")) | |
| 420 | + .contains("LeveledCompactionStrategy"); | |
| 421 | + } | |
| 416 | 422 | for (Metadata m : metadatas()) |
| 417 | 423 | assertThat(m.getKeyspace(keyspace).getMaterializedView("mv1").getOptions().getCompaction().get("class")).contains("LeveledCompactionStrategy"); |
| 418 | 424 | } |
| @@ -423,7 +429,12 @@ | ||
| 423 | 429 | session1.execute(String.format("CREATE TABLE %s.table1 (pk int PRIMARY KEY, c int)", keyspace)); |
| 424 | 430 | session1.execute(String.format("CREATE MATERIALIZED VIEW %s.mv1 AS SELECT c FROM %s.table1 WHERE c IS NOT NULL PRIMARY KEY (pk, c)", keyspace, keyspace)); |
| 425 | 431 | session1.execute(String.format("DROP MATERIALIZED VIEW %s.mv1", keyspace)); |
| 426 | - refreshSchema(); | |
| 432 | + for (SchemaChangeListener listener : listeners) { | |
| 433 | + ArgumentCaptor<MaterializedViewMetadata> removed = ArgumentCaptor.forClass(MaterializedViewMetadata.class); | |
| 434 | + verify(listener, timeout(NOTIF_TIMEOUT_MS).times(1)).onMaterializedViewRemoved(removed.capture()); | |
| 435 | + assertThat(removed.getValue()) | |
| 436 | + .hasName("mv1"); | |
| 437 | + } | |
| 427 | 438 | for (Metadata m : metadatas()) |
| 428 | 439 | assertThat(m.getKeyspace(keyspace).getMaterializedView("mv1")).isNull(); |
| 429 | 440 | } |
| @@ -436,7 +447,6 @@ | ||
| 436 | 447 | verify(listener, timeout(NOTIF_TIMEOUT_MS).times(1)).onKeyspaceAdded(added.capture()); |
| 437 | 448 | assertThat(added.getValue()).hasName(handleId(keyspace)); |
| 438 | 449 | } |
| 439 | - refreshSchema(); | |
| 440 | 450 | for (Metadata m : metadatas()) |
| 441 | 451 | assertThat(m.getKeyspace(keyspace)).isNotNull(); |
| 442 | 452 | } |
| @@ -451,7 +461,6 @@ | ||
| 451 | 461 | assertThat(added.getValue()).hasName(handleId(keyspace)); |
| 452 | 462 | } |
| 453 | 463 | assert added != null; |
| 454 | - refreshSchema(); | |
| 455 | 464 | for (Metadata m : metadatas()) |
| 456 | 465 | assertThat(m.getKeyspace(keyspace).isDurableWrites()).isTrue(); |
| 457 | 466 | execute(ALTER_KEYSPACE, keyspace); |
| @@ -466,7 +475,6 @@ | ||
| 466 | 475 | .hasName(handleId(keyspace)) |
| 467 | 476 | .isNotDurableWrites(); |
| 468 | 477 | } |
| 469 | - refreshSchema(); | |
| 470 | 478 | for (Metadata m : metadatas()) |
| 471 | 479 | assertThat(m.getKeyspace(keyspace)).isNotDurableWrites(); |
| 472 | 480 | } |
| @@ -474,14 +482,11 @@ | ||
| 474 | 482 | @Test(groups = "short", dataProvider = "newKeyspaceName") |
| 475 | 483 | public void should_notify_of_keyspace_drop(String keyspace) throws InterruptedException { |
| 476 | 484 | execute(CREATE_KEYSPACE, keyspace); |
| 477 | - ArgumentCaptor<KeyspaceMetadata> added = null; | |
| 478 | 485 | for (SchemaChangeListener listener : listeners) { |
| 479 | - added = ArgumentCaptor.forClass(KeyspaceMetadata.class); | |
| 486 | + ArgumentCaptor<KeyspaceMetadata> added = ArgumentCaptor.forClass(KeyspaceMetadata.class); | |
| 480 | 487 | verify(listener, timeout(NOTIF_TIMEOUT_MS).times(1)).onKeyspaceAdded(added.capture()); |
| 481 | 488 | assertThat(added.getValue()).hasName(handleId(keyspace)); |
| 482 | 489 | } |
| 483 | - assert added != null; | |
| 484 | - refreshSchema(); | |
| 485 | 490 | for (Metadata m : metadatas()) |
| 486 | 491 | assertThat(m.getReplicas(keyspace, Bytes.fromHexString("0xCAFEBABE"))).isNotEmpty(); |
| 487 | 492 | execute(CREATE_TABLE, keyspace); // to test table drop notifications |
| @@ -497,14 +502,12 @@ | ||
| 497 | 502 | assertThat(ks.getValue()) |
| 498 | 503 | .hasName(handleId(keyspace)); |
| 499 | 504 | } |
| 500 | - refreshSchema(); | |
| 501 | 505 | for (Metadata m : metadatas()) { |
| 502 | 506 | assertThat(m.getKeyspace(keyspace)).isNull(); |
| 503 | 507 | assertThat(m.getReplicas(keyspace, Bytes.fromHexString("0xCAFEBABE"))).isEmpty(); |
| 504 | 508 | } |
| 505 | 509 | } |
| 506 | 510 | |
| 507 | - | |
| 508 | 511 | /** |
| 509 | 512 | * Ensures that calling {@link Metadata#newToken(String)} on a Cluster that has schema |
| 510 | 513 | * metadata disabled will throw a {@link IllegalStateException}. |
| @@ -588,7 +591,6 @@ | ||
| 588 | 591 | schemaDisabledCluster.getConfiguration().getQueryOptions().setMetadataEnabled(true); |
| 589 | 592 | |
| 590 | 593 | verify(schemaDisabledControlConnection, after(1000)).refreshSchema(null, null, null, null); |
| 591 | - verify(schemaDisabledControlConnection).refreshNodeListAndTokenMap(); | |
| 592 | 594 | |
| 593 | 595 | // Ensure that there is schema metadata. |
| 594 | 596 | assertThat(schemaDisabledCluster.getMetadata().getKeyspace(keyspace)).isNotNull(); |
| @@ -604,7 +606,6 @@ | ||
| 604 | 606 | reset(schemaDisabledControlConnection); |
| 605 | 607 | schemaDisabledCluster.getConfiguration().getQueryOptions().setMetadataEnabled(true); |
| 606 | 608 | verify(schemaDisabledControlConnection, after(1000).never()).refreshSchema(null, null, null, null); |
| 607 | - verify(schemaDisabledControlConnection, never()).refreshNodeListAndTokenMap(); | |
| 608 | 609 | } finally { |
| 609 | 610 | // Reset listener mock to not count it's interactions in this test. |
| 610 | 611 | reset(schemaDisabledListener); |
| @@ -643,13 +644,4 @@ | ||
| 643 | 644 | return Lists.newArrayList(cluster1.getMetadata(), cluster2.getMetadata()); |
| 644 | 645 | } |
| 645 | 646 | |
| 646 | - @SuppressWarnings("unchecked") | |
| 647 | - private void refreshSchema() { | |
| 648 | - Futures.getUnchecked( | |
| 649 | - Futures.successfulAsList( | |
| 650 | - cluster1.manager.submitSchemaRefresh(null, null, null, null), | |
| 651 | - cluster2.manager.submitSchemaRefresh(null, null, null, null))); | |
| 652 | - } | |
| 653 | - | |
| 654 | - | |
| 655 | 647 | } |