mirror of
https://github.com/honeymoose/OpenSearch.git
synced 2025-02-09 06:25:07 +00:00
Harish Kayarohanam
3976854ada
Improve error handling of ClassCastException in terms aggregations.
What is the problem we are trying to solve ? =========================================== When we are doing aggregations against a field name as shown in https://github.com/HarishAtGitHub/elasticsearch-tester/blob/master/12135.py#L37-L46 search = { "aggs": { "NAME": { "terms": { "field": "ip_str", "size": 10 } } } } and when the field "ip_str" has values of different types in different indices . say one is of type StringTerms type and other is of IP(LongTerms type) then the aggregation fails as the types do not match(incompatible). The failure throws a class cast exception as follows: { "error": { "root_cause": [], "type": "reduce_search_phase_exception", "reason": "[reduce] ", "phase": "query", "grouped": true, "failed_shards": [], "caused_by": { "type": "class_cast_exception", "reason": "org.elasticsearch.search.aggregations.bucket.terms.LongTerms$Bucket cannot be cast to org.elasticsearch.search.aggregations.bucket.terms.StringTerms$Bucket" } }, "status": 503 } which is hard to understand . User cannot infer anything about the cause of the problem and what he should do from seeing the class cast exception. What can be the possible solution ? =================================== Make the exception more readable by showing him the root cause of the problem so that he can understand which area actually caused the problem, so that he can take necessary steps further. Code Analysis ============= Debugging code shows that: the query /{indices}/_search?search_type=count involves two phases 1) search phase *************** searchService.sendExecuteQuery(...) [Ref: TransportSearchCountAction] what happens here ? the phase 1, which is the search phase goes without error. In this phase the shards for the given indexes are collected and the search is done on all asynchronously and finally collected in the variable "firstResults" and given to meger phase. [Flow: .... -> TransportSearchTypeAction -> method performFirstPhase] 2) merge phase ************** searchPhaseController.merge(...firstResults...) [Ref: TransportSearchCountAction] what happens here ? the "firstresults" QuerySearchResults are now to be aggregated and combined. [Flow: SearchPhaseController.merge(...) -> ..... -> InternalTerms.doReduce(...)] the phase 1, which is the search phase goes without error. The problem comes in phase 2, which is merge phase. Now the individual term buckets are available. As per the test case , there are two indices cast and cast2, so by default 10 shards. cast has ip_str of type StringTerms cast2 has ip_str of type ip which is actually LongTerms so here two types of Buckets exist. StringTerms_Bucket and LongTerms_Bucket. Now the aggregation is to be put inside the BucketPriorityQueue(size 2: as out of 10, 2 has hits) finally. (docs of PriorityQueue: https://lucene.apache.org/core/4_4_0/core/org/apache/lucene/util/PriorityQueue.html#insertWithOverflow(T)) Now first the LongTerms$Bucket is put inside. then the StringTerms$Bucket is to be put in. This is the area where exception is thrown. What happens is when adding the StringTerms$Bucket now it has to goes through the code "lessThan(element, heap[1])" which finally calls --------------------------------------------------------------------------------------------- | StringTerms$Bucket.compareTerms(other) <---------------- Area of exception | | | -------------------------------------------------------------------------------------------- where when comparing one to other a type cast is done and it fails as StringTerms$Bucket and LongTerms$Bucket are incompatible. Approach to solve: ================== The best way is to make user understand that the problem is when reducing/merging/aggregating the buckets which came as a result of querying different shards, so that this will make them infer that the problem is because the values of the fields are of different types. The message is also user friendly and much better than the indecipherable classcastexception. The only place to infer correctly that the aggregation has failed is in the place where aggregations take place. so at InternalTerms.java -> (BucketPriorityQueue)ordered.insertWithOverflow(b); so here I can throw AggregationExecutionException saying it is because the buckets are of different types. But when can I infer at this point that the failure is due to mismatch of types of buckets ??? it can be possible only if at this point it is informed that the problem which occurred deep inside is due to buckets that were incomparable. so from just a classCastException we cannot make such a pointed exact inference, because as class cast exception can be due to a number of scenarios and at a number of places. so unless we inform the exact problem to InternalTerms it will not be able to infer properly. so infer the classCastException at the compareTerms function itself that it is a IncomparableTermBucktesTypeException. This is the best place to infer classCastException as this the place which generated the exception. Best inference of exceptions can be done only at the source/origin of the exception. so IncomparableTermBucktesTypeException to InternalTerms-> will make it infer and conclude on why aggregation failed and give best information to user. Close #12821
h1. Elasticsearch h2. A Distributed RESTful Search Engine h3. "https://www.elastic.co/products/elasticsearch":https://www.elastic.co/products/elasticsearch Elasticsearch is a distributed RESTful search engine built for the cloud. Features include: * Distributed and Highly Available Search Engine. ** Each index is fully sharded with a configurable number of shards. ** Each shard can have one or more replicas. ** Read / Search operations performed on either one of the replica shard. * Multi Tenant with Multi Types. ** Support for more than one index. ** Support for more than one type per index. ** Index level configuration (number of shards, index storage, ...). * Various set of APIs ** HTTP RESTful API ** Native Java API. ** All APIs perform automatic node operation rerouting. * Document oriented ** No need for upfront schema definition. ** Schema can be defined per type for customization of the indexing process. * Reliable, Asynchronous Write Behind for long term persistency. * (Near) Real Time Search. * Built on top of Lucene ** Each shard is a fully functional Lucene index ** All the power of Lucene easily exposed through simple configuration / plugins. * Per operation consistency ** Single document level operations are atomic, consistent, isolated and durable. * Open Source under the Apache License, version 2 ("ALv2") h2. Getting Started First of all, DON'T PANIC. It will take 5 minutes to get the gist of what Elasticsearch is all about. h3. Requirements You need to have a recent version of Java installed. See the "Setup":http://www.elastic.co/guide/en/elasticsearch/reference/current/setup.html#jvm-version page for more information. h3. Installation * "Download":https://www.elastic.co/downloads/elasticsearch and unzip the Elasticsearch official distribution. * Run @bin/elasticsearch@ on unix, or @bin\elasticsearch.bat@ on windows. * Run @curl -X GET http://127.0.0.1:9200/@. * Start more servers ... h3. Indexing Let's try and index some twitter like information. First, let's create a twitter user, and add some tweets (the @twitter@ index will be created automatically): <pre> curl -XPUT 'http://127.0.0.1:9200/twitter/user/kimchy' -d '{ "name" : "Shay Banon" }' curl -XPUT 'http://127.0.0.1:9200/twitter/tweet/1' -d ' { "user": "kimchy", "postDate": "2009-11-15T13:12:00", "message": "Trying out Elasticsearch, so far so good?" }' curl -XPUT 'http://127.0.0.1:9200/twitter/tweet/2' -d ' { "user": "kimchy", "postDate": "2009-11-15T14:12:12", "message": "Another tweet, will it be indexed?" }' </pre> Now, let's see if the information was added by GETting it: <pre> curl -XGET 'http://127.0.0.1:9200/twitter/user/kimchy?pretty=true' curl -XGET 'http://127.0.0.1:9200/twitter/tweet/1?pretty=true' curl -XGET 'http://127.0.0.1:9200/twitter/tweet/2?pretty=true' </pre> h3. Searching Mmm search..., shouldn't it be elastic? Let's find all the tweets that @kimchy@ posted: <pre> curl -XGET 'http://127.0.0.1:9200/twitter/tweet/_search?q=user:kimchy&pretty=true' </pre> We can also use the JSON query language Elasticsearch provides instead of a query string: <pre> curl -XGET 'http://127.0.0.1:9200/twitter/tweet/_search?pretty=true' -d ' { "query" : { "match" : { "user": "kimchy" } } }' </pre> Just for kicks, let's get all the documents stored (we should see the user as well): <pre> curl -XGET 'http://127.0.0.1:9200/twitter/_search?pretty=true' -d ' { "query" : { "matchAll" : {} } }' </pre> We can also do range search (the @postDate@ was automatically identified as date) <pre> curl -XGET 'http://127.0.0.1:9200/twitter/_search?pretty=true' -d ' { "query" : { "range" : { "postDate" : { "from" : "2009-11-15T13:00:00", "to" : "2009-11-15T14:00:00" } } } }' </pre> There are many more options to perform search, after all, it's a search product no? All the familiar Lucene queries are available through the JSON query language, or through the query parser. h3. Multi Tenant - Indices and Types Maan, that twitter index might get big (in this case, index size == valuation). Let's see if we can structure our twitter system a bit differently in order to support such large amounts of data. Elasticsearch supports multiple indices, as well as multiple types per index. In the previous example we used an index called @twitter@, with two types, @user@ and @tweet@. Another way to define our simple twitter system is to have a different index per user (note, though that each index has an overhead). Here is the indexing curl's in this case: <pre> curl -XPUT 'http://127.0.0.1:9200/kimchy/info/1' -d '{ "name" : "Shay Banon" }' curl -XPUT 'http://127.0.0.1:9200/kimchy/tweet/1' -d ' { "user": "kimchy", "postDate": "2009-11-15T13:12:00", "message": "Trying out Elasticsearch, so far so good?" }' curl -XPUT 'http://127.0.0.1:9200/kimchy/tweet/2' -d ' { "user": "kimchy", "postDate": "2009-11-15T14:12:12", "message": "Another tweet, will it be indexed?" }' </pre> The above will index information into the @kimchy@ index, with two types, @info@ and @tweet@. Each user will get his own special index. Complete control on the index level is allowed. As an example, in the above case, we would want to change from the default 5 shards with 1 replica per index, to only 1 shard with 1 replica per index (== per twitter user). Here is how this can be done (the configuration can be in yaml as well): <pre> curl -XPUT http://127.0.0.1:9200/another_user/ -d ' { "index" : { "numberOfShards" : 1, "numberOfReplicas" : 1 } }' </pre> Search (and similar operations) are multi index aware. This means that we can easily search on more than one index (twitter user), for example: <pre> curl -XGET 'http://127.0.0.1:9200/kimchy,another_user/_search?pretty=true' -d ' { "query" : { "matchAll" : {} } }' </pre> Or on all the indices: <pre> curl -XGET 'http://127.0.0.1:9200/_search?pretty=true' -d ' { "query" : { "matchAll" : {} } }' </pre> {One liner teaser}: And the cool part about that? You can easily search on multiple twitter users (indices), with different boost levels per user (index), making social search so much simpler (results from my friends rank higher than results from friends of my friends). h3. Distributed, Highly Available Let's face it, things will fail.... Elasticsearch is a highly available and distributed search engine. Each index is broken down into shards, and each shard can have one or more replica. By default, an index is created with 5 shards and 1 replica per shard (5/1). There are many topologies that can be used, including 1/10 (improve search performance), or 20/1 (improve indexing performance, with search executed in a map reduce fashion across shards). In order to play with the distributed nature of Elasticsearch, simply bring more nodes up and shut down nodes. The system will continue to serve requests (make sure you use the correct http port) with the latest data indexed. h3. Where to go from here? We have just covered a very small portion of what Elasticsearch is all about. For more information, please refer to the "elastic.co":http://www.elastic.co/products/elasticsearch website. h3. Building from Source Elasticsearch uses "Maven":http://maven.apache.org for its build system. In order to create a distribution, simply run the @mvn clean package -DskipTests@ command in the cloned directory. The distribution for each project will be created under the @target/releases@ directory in that project. See the "TESTING":TESTING.asciidoc file for more information about running the Elasticsearch test suite. h3. Upgrading to Elasticsearch 1.x? In order to ensure a smooth upgrade process from earlier versions of Elasticsearch (< 1.0.0), it is recommended to perform a full cluster restart. Please see the "setup reference":https://www.elastic.co/guide/en/elasticsearch/reference/current/setup-upgrade.html for more details on the upgrade process. h1. License <pre> This software is licensed under the Apache License, version 2 ("ALv2"), quoted below. Copyright 2009-2015 Elasticsearch <https://www.elastic.co> Licensed 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. </pre>
Description
Languages
Java
99.5%
Groovy
0.4%