Skip to content

Commit

Permalink
added number_of_shards and number_of_replicas to information_schema.t…
Browse files Browse the repository at this point in the history
…able_partitions

and exposed these information in Map<PartitionName, PartitionInfo> returned by TableInfo.partitions()
  • Loading branch information
msbt committed Feb 27, 2015
1 parent fb817d1 commit 5d60609
Show file tree
Hide file tree
Showing 25 changed files with 772 additions and 252 deletions.
3 changes: 3 additions & 0 deletions CHANGES.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ Changes for Crate
Unreleased
==========

- Added columns ``number_of_shards`` and ``number_of_replicas`` to
table ``information_schema.table_partitions``

- Support `ON DUPLICATE KEY UPDATE` while inserting using a query.

- Fix: Parameter substitution within subscript notation is not
Expand Down
24 changes: 22 additions & 2 deletions core/src/main/java/io/crate/core/NumberOfReplicas.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,12 @@

package io.crate.core;

import com.google.common.base.MoreObjects;
import com.google.common.base.Preconditions;
import org.apache.lucene.util.BytesRef;
import org.elasticsearch.cluster.metadata.IndexMetaData;
import org.elasticsearch.common.Booleans;
import org.elasticsearch.common.settings.Settings;

import java.util.regex.Pattern;

Expand All @@ -42,18 +46,34 @@ public NumberOfReplicas(Integer replicas) {

public NumberOfReplicas(String replicas) {
assert replicas != null;
Preconditions.checkArgument(EXPAND_REPLICA_PATTERN.matcher(replicas).matches(),
"The \"number_of_replicas\" range \"%s\" isn't valid", replicas);
validateExpandReplicaSetting(replicas);

this.esSettingKey = AUTO_EXPAND_REPLICAS;
this.esSettingsValue = replicas;
}

private static void validateExpandReplicaSetting(String replicas) {
Preconditions.checkArgument(EXPAND_REPLICA_PATTERN.matcher(replicas).matches(),
"The \"number_of_replicas\" range \"%s\" isn't valid", replicas);
}

public String esSettingKey() {
return esSettingKey;
}

public String esSettingValue() {
return esSettingsValue;
}

public static BytesRef fromSettings(Settings settings) {
BytesRef numberOfReplicas;
String autoExpandReplicas = settings.get(AUTO_EXPAND_REPLICAS);
if (autoExpandReplicas != null && !Booleans.isExplicitFalse(autoExpandReplicas)) {
validateExpandReplicaSetting(autoExpandReplicas);
numberOfReplicas = new BytesRef(autoExpandReplicas);
} else {
numberOfReplicas = new BytesRef(MoreObjects.firstNonNull(settings.get(NUMBER_OF_REPLICAS), "1"));
}
return numberOfReplicas;
}
}
70 changes: 70 additions & 0 deletions core/src/test/java/io/crate/core/NumberOfReplicasTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
/*
* 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.RandomizedTest;
import org.apache.lucene.util.BytesRef;
import org.elasticsearch.common.settings.ImmutableSettings;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;

import static org.hamcrest.Matchers.is;

public class NumberOfReplicasTest extends RandomizedTest {

@Rule
public ExpectedException expectedException = ExpectedException.none();

@Test
public void testFromEmptySettings() throws Exception {
BytesRef numberOfResplicas = NumberOfReplicas.fromSettings(ImmutableSettings.EMPTY);
assertThat(numberOfResplicas.utf8ToString(), is("1"));
}

@Test
public void testNumber() throws Exception {
BytesRef numberOfResplicas = NumberOfReplicas.fromSettings(ImmutableSettings.builder()
.put(NumberOfReplicas.NUMBER_OF_REPLICAS, 4)
.build());
assertThat(numberOfResplicas.utf8ToString(), is("4"));
}

@Test
public void testAutoExpandSettingsTakePrecedence() throws Exception {
BytesRef numberOfResplicas = NumberOfReplicas.fromSettings(ImmutableSettings.builder()
.put(NumberOfReplicas.AUTO_EXPAND_REPLICAS, "0-all")
.put(NumberOfReplicas.NUMBER_OF_REPLICAS, 1)
.build());
assertThat(numberOfResplicas.utf8ToString(), is("0-all"));
}

@Test
public void testInvalidAutoExpandSettings() throws Exception {
expectedException.expect(IllegalArgumentException.class);
expectedException.expectMessage("The \"number_of_replicas\" range \"abc\" isn't valid");
NumberOfReplicas.fromSettings(ImmutableSettings.builder()
.put(NumberOfReplicas.AUTO_EXPAND_REPLICAS, "abc")
.put(NumberOfReplicas.NUMBER_OF_REPLICAS, 1)
.build());
}
}
12 changes: 6 additions & 6 deletions docs/sql/dml.txt
Original file line number Diff line number Diff line change
Expand Up @@ -125,12 +125,12 @@ Resulting partitions of the last insert by query::
cr> select * from information_schema.table_partitions
... where table_name = 'locations_parted'
... order by partition_ident;
+------------------+-------------+-----------------+------------------+
| table_name | schema_name | partition_ident | values |
+------------------+-------------+-----------------+------------------+
| locations_parted | doc | 042j2e9n74 | {"year": "1979"} |
| locations_parted | doc | 042j4c1h6c | {"year": "2013"} |
+------------------+-------------+-----------------+------------------+
+------------------+-------------+-----------------+------------------+------------------+--------------------+
| table_name | schema_name | partition_ident | values | number_of_shards | number_of_replicas |
+------------------+-------------+-----------------+------------------+------------------+--------------------+
| locations_parted | doc | 042j2e9n74 | {"year": "1979"} | 2 | 0 |
| locations_parted | doc | 042j4c1h6c | {"year": "2013"} | 2 | 0 |
+------------------+-------------+-----------------+------------------+------------------+--------------------+
SELECT 2 rows in set (... sec)

.. Hidden: drop previously created table
Expand Down
24 changes: 23 additions & 1 deletion docs/sql/information_schema.txt
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,11 @@ For further information see :ref:`sql_ddl_partitioned_by`.
cr> insert into a_partitioned_table (id, content) values (1, 'content_a');
INSERT OK, 1 row affected (... sec)

::

cr> alter table a_partitioned_table set (number_of_shards=4);
ALTER OK (... sec)

::

cr> insert into a_partitioned_table (id, content) values (2, 'content_b');
Expand All @@ -187,7 +192,7 @@ For further information see :ref:`sql_ddl_partitioned_by`.
The following example shows a table where the column 'content' of table
'a_partitioned_table' has been used to partition the table. The table has two
partitions. The partitions are introduced when data is inserted where
'content' is 'content_a', and 'content_b'::
'content' is 'content_a', and 'content_b'.::

cr> select table_name, schema_name as schema, partition_ident, "values"
... from information_schema.table_partitions
Expand All @@ -200,6 +205,23 @@ partitions. The partitions are introduced when data is inserted where
+---------------------+--------+--------------------+--------------------------+
SELECT 2 rows in set (... sec)

The second partition has been created
after the number of shards for future partitions have been changed on the
partitioned table, so they show ``4`` instead of ``5``::

cr> select table_name, partition_ident,
... number_of_shards, number_of_replicas
... from information_schema.table_partitions
... order by table_name, partition_ident;
+---------------------+--------------------+------------------+--------------------+
| table_name | partition_ident | number_of_shards | number_of_replicas |
+---------------------+--------------------+------------------+--------------------+
| a_partitioned_table | 04566rreehimst2vc4 | 5 | 1 |
| a_partitioned_table | 04566rreehimst2vc8 | 4 | 1 |
+---------------------+--------------------+------------------+--------------------+
SELECT 2 rows in set (... sec)



Routines
========
Expand Down
37 changes: 25 additions & 12 deletions docs/sql/partitioned_tables.txt
Original file line number Diff line number Diff line change
Expand Up @@ -122,15 +122,15 @@ Insert

::

cr> SELECT partition_ident, "values"
cr> SELECT partition_ident, "values", number_of_shards
... FROM information_schema.table_partitions
... WHERE schema_name = 'doc' AND table_name = 'parted_table'
... ORDER BY partition_ident;
+--------------------------+------------------------+
| partition_ident | values |
+--------------------------+------------------------+
| 04732cpp6osj2d9i60o30c1g | {"day": 1396915200000} |
+--------------------------+------------------------+
+--------------------------+------------------------+------------------+
| partition_ident | values | number_of_shards |
+--------------------------+------------------------+------------------+
| 04732cpp6osj2d9i60o30c1g | {"day": 1396915200000} | 4 |
+--------------------------+------------------------+------------------+
SELECT 1 row in set (... sec)

On subsequent inserts with the same ``PARTITIONED BY`` column values,
Expand All @@ -147,15 +147,15 @@ no additional partition is created::

::

cr> SELECT partition_ident, "values"
cr> SELECT partition_ident, "values", number_of_shards
... FROM information_schema.table_partitions
... WHERE schema_name = 'doc' AND table_name = 'parted_table'
... ORDER BY partition_ident;
+--------------------------+------------------------+
| partition_ident | values |
+--------------------------+------------------------+
| 04732cpp6osj2d9i60o30c1g | {"day": 1396915200000} |
+--------------------------+------------------------+
+--------------------------+------------------------+------------------+
| partition_ident | values | number_of_shards |
+--------------------------+------------------------+------------------+
| 04732cpp6osj2d9i60o30c1g | {"day": 1396915200000} | 4 |
+--------------------------+------------------------+------------------+
SELECT 1 row in set (... sec)


Expand Down Expand Up @@ -278,6 +278,19 @@ on **future** partitions.
+------------+----------+
SELECT 1 row in set (... sec)

::

cr> SELECT partition_ident, "values", number_of_shards
... FROM information_schema.table_partitions
... WHERE schema_name = 'doc' AND table_name = 'parted_table'
... ORDER BY partition_ident;
+--------------------------+------------------------+------------------+
| partition_ident | values | number_of_shards |
+--------------------------+------------------------+------------------+
| 04732cpp6osj2d9i60o30c1g | {"day": 1396915200000} | 10 |
+--------------------------+------------------------+------------------+
SELECT 1 row in set (... sec)

Alter Partitions
----------------

Expand Down
91 changes: 91 additions & 0 deletions sql/src/main/java/io/crate/metadata/PartitionInfo.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
/*
* 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.metadata;

import com.google.common.base.MoreObjects;
import org.apache.lucene.util.BytesRef;

import java.util.Map;

public class PartitionInfo {
private final PartitionName name;
private final int numberOfShards;
private final BytesRef numberOfReplicas;
private final Map<String, Object> values;

public PartitionInfo(PartitionName name,
int numberOfShards,
BytesRef numberOfReplicas,
Map<String, Object> values) {
this.name = name;
this.numberOfShards = numberOfShards;
this.numberOfReplicas = numberOfReplicas;
this.values = values;
}

public PartitionName name() {
return name;
}

public int numberOfShards() {
return numberOfShards;
}

public BytesRef numberOfReplicas() {
return numberOfReplicas;
}

public Map<String, Object> values() {
return values;
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;

PartitionInfo that = (PartitionInfo) o;

if (!name.equals(that.name)) return false;
if (numberOfReplicas != that.numberOfReplicas) return false;
if (numberOfShards != that.numberOfShards) return false;

return true;
}

@Override
public int hashCode() {
int result = name.hashCode();
result = 31 * result + numberOfShards;
result = 31 * result + numberOfReplicas.hashCode();
return result;
}

@Override
public String toString() {
return MoreObjects.toStringHelper(this)
.add("name", name)
.add("numberOfShards", numberOfShards)
.add("numberOfReplicas", numberOfReplicas)
.toString();
}
}
Loading

0 comments on commit 5d60609

Please sign in to comment.