diff options
author | Stephane Gamard <stephane.gamard@searchbox.com> | 2014-05-01 13:11:20 +0200 |
---|---|---|
committer | Stephane Gamard <stephane.gamard@searchbox.com> | 2014-05-01 13:11:20 +0200 |
commit | 2777ac53327d7e2369e00704bfa87f48eb4e04a6 (patch) | |
tree | 45675686e82927a5dbce705ec43e7c63af750fc9 /sonar-server | |
parent | d50cbc6b72ab364a2c8dc708c5e3473db7a471b0 (diff) | |
download | sonarqube-2777ac53327d7e2369e00704bfa87f48eb4e04a6.tar.gz sonarqube-2777ac53327d7e2369e00704bfa87f48eb4e04a6.zip |
SONAR-5237 - Implemented IndexAction compatible with Dto
- Fixed missing public Field for WS in RuleIndex
Diffstat (limited to 'sonar-server')
10 files changed, 196 insertions, 65 deletions
diff --git a/sonar-server/src/main/java/org/sonar/server/cluster/LocalQueueWorker.java b/sonar-server/src/main/java/org/sonar/server/cluster/LocalQueueWorker.java index ba4c60ff7b0..b54cdf49110 100644 --- a/sonar-server/src/main/java/org/sonar/server/cluster/LocalQueueWorker.java +++ b/sonar-server/src/main/java/org/sonar/server/cluster/LocalQueueWorker.java @@ -36,14 +36,14 @@ public class LocalQueueWorker extends ThreadPoolExecutor private static final Logger LOG = LoggerFactory.getLogger(LocalQueueWorker.class); - private Map<String, Index<?>> indexes; + private Map<String, Index> indexes; - public LocalQueueWorker(LocalNonBlockingWorkQueue queue, Index<?>... allIndexes) { + public LocalQueueWorker(LocalNonBlockingWorkQueue queue, Index... allIndexes) { super(10, 10, 500l, TimeUnit.MILLISECONDS, queue); // Save all instances of Index<?> - this.indexes = new HashMap<String, Index<?>>(); - for (Index<?> index : allIndexes) { + this.indexes = new HashMap<String, Index>(); + for (Index index : allIndexes) { this.indexes.put(index.getIndexName(), index); } } @@ -52,7 +52,7 @@ public class LocalQueueWorker extends ThreadPoolExecutor LOG.debug("Starting task: {}",r); super.beforeExecute(t, r); if(r.getClass().isAssignableFrom(IndexAction.class)){ - IndexAction<?> ia = (IndexAction<?>)r; + IndexAction ia = (IndexAction)r; LOG.trace("Task is an IndexAction for {}",ia.getIndexName()); ia.setIndex(indexes.get(ia.getIndexName())); } diff --git a/sonar-server/src/main/java/org/sonar/server/db/BaseDao.java b/sonar-server/src/main/java/org/sonar/server/db/BaseDao.java index fe4e7381dac..9db15bace32 100644 --- a/sonar-server/src/main/java/org/sonar/server/db/BaseDao.java +++ b/sonar-server/src/main/java/org/sonar/server/db/BaseDao.java @@ -65,9 +65,10 @@ public abstract class BaseDao<E extends Dto<K>, K extends Serializable> @Override public E update(E item, DbSession session) { + this.doUpdate(item, session); session.enqueue(new IndexAction(this.getIndexName(), - IndexAction.Method.UPDATE, item.getKey())); - return this.doUpdate(item, session); + IndexAction.Method.UPDATE, item)); + return item; } @Override @@ -84,10 +85,9 @@ public abstract class BaseDao<E extends Dto<K>, K extends Serializable> @Override public E insert(E item, DbSession session) { - IndexAction action = new IndexAction(this.getIndexName(), - IndexAction.Method.INSERT, item.getKey()); - session.enqueue(action); this.doInsert(item, session); + session.enqueue(new IndexAction(this.getIndexName(), + IndexAction.Method.INSERT, item)); return item; } diff --git a/sonar-server/src/main/java/org/sonar/server/rule2/RuleDoc.java b/sonar-server/src/main/java/org/sonar/server/rule2/RuleDoc.java index 67914d753b0..48402175569 100644 --- a/sonar-server/src/main/java/org/sonar/server/rule2/RuleDoc.java +++ b/sonar-server/src/main/java/org/sonar/server/rule2/RuleDoc.java @@ -120,6 +120,6 @@ class RuleDoc implements Rule { @Override public Date updatedAt() { - return (Date) fields.get(RuleField.UDPATED_AT.key()); + return (Date) fields.get(RuleField.UPDATED_AT.key()); } } diff --git a/sonar-server/src/main/java/org/sonar/server/rule2/RuleIndex.java b/sonar-server/src/main/java/org/sonar/server/rule2/RuleIndex.java index 6bdf048e04d..3b6a02b672b 100644 --- a/sonar-server/src/main/java/org/sonar/server/rule2/RuleIndex.java +++ b/sonar-server/src/main/java/org/sonar/server/rule2/RuleIndex.java @@ -64,7 +64,8 @@ public class RuleIndex extends BaseIndex<RuleKey, RuleDto> { RuleField.TAGS.key(), RuleField.SYSTEM_TAGS.key(), RuleField.CREATED_AT.key(), - RuleField.UDPATED_AT.key()); + RuleField.REPOSITORY.key(), + RuleField.UPDATED_AT.key()); public RuleIndex(RuleNormalizer normalizer, WorkQueue workQueue, Profiling profiling, ESNode node) { diff --git a/sonar-server/src/main/java/org/sonar/server/rule2/RuleNormalizer.java b/sonar-server/src/main/java/org/sonar/server/rule2/RuleNormalizer.java index 85b03c23e0a..27e8a348673 100644 --- a/sonar-server/src/main/java/org/sonar/server/rule2/RuleNormalizer.java +++ b/sonar-server/src/main/java/org/sonar/server/rule2/RuleNormalizer.java @@ -20,6 +20,8 @@ package org.sonar.server.rule2; import org.elasticsearch.common.xcontent.XContentBuilder; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import org.sonar.api.rule.RuleKey; import org.sonar.check.Cardinality; import org.sonar.core.rule.RuleDto; @@ -31,6 +33,8 @@ import static org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder; public class RuleNormalizer extends BaseNormalizer<RuleDto, RuleKey> { + private static final Logger LOG = LoggerFactory.getLogger(RuleNormalizer.class); + private RuleDao ruleDao; public enum RuleField { @@ -46,7 +50,7 @@ public class RuleNormalizer extends BaseNormalizer<RuleDto, RuleKey> { SYSTEM_TAGS("sysTags"), INTERNAL_KEY("internalKey"), TEMPLATE("template"), - UDPATED_AT("updatedAt"); + UPDATED_AT("updatedAt"); private final String key; @@ -79,7 +83,7 @@ public class RuleNormalizer extends BaseNormalizer<RuleDto, RuleKey> { indexField(RuleField.REPOSITORY.key(), rule.getRepositoryKey(), document); indexField(RuleField.NAME.key(), rule.getName(), document); indexField(RuleField.CREATED_AT.key(), rule.getCreatedAt(), document); - indexField(RuleField.UDPATED_AT.key(), rule.getUpdatedAt(), document); + indexField(RuleField.UPDATED_AT.key(), rule.getUpdatedAt(), document); indexField(RuleField.DESCRIPTION.key(), rule.getDescription(), document); indexField(RuleField.SEVERITY.key(), rule.getSeverityString(), document); indexField(RuleField.STATUS.key(), rule.getStatus(), document); diff --git a/sonar-server/src/main/java/org/sonar/server/search/BaseIndex.java b/sonar-server/src/main/java/org/sonar/server/search/BaseIndex.java index d032be44617..d41f847e5c7 100644 --- a/sonar-server/src/main/java/org/sonar/server/search/BaseIndex.java +++ b/sonar-server/src/main/java/org/sonar/server/search/BaseIndex.java @@ -38,8 +38,9 @@ import org.sonar.server.es.ESNode; import java.io.IOException; import java.io.Serializable; import java.util.Collection; +import java.util.concurrent.ExecutionException; -public abstract class BaseIndex<K extends Serializable, E extends Dto<K>> implements Index<K> { +public abstract class BaseIndex<K extends Serializable, E extends Dto<K>> implements Index<E, K> { private static final Logger LOG = LoggerFactory.getLogger(BaseIndex.class); @@ -124,19 +125,52 @@ public abstract class BaseIndex<K extends Serializable, E extends Dto<K>> implem return Hit.fromMap(0, result.getSourceAsMap()); } + private void insertDocument(XContentBuilder doc, String key) throws ExecutionException, InterruptedException { + LOG.debug("INSERT _id:{} in index {}", key, this.getIndexName()); + getClient().index( + new IndexRequest(this.getIndexName(), + getType(), key) + .source(doc) + ).get(); + } + + @Override + public void insert(Object obj) throws InvalidIndexActionException { + LOG.info("INSERT for class" + obj.getClass().getSimpleName()); + if(Dto.class.isAssignableFrom(obj.getClass())){ + this.insertByDto((E) obj); + } else if(Serializable.class.isAssignableFrom(obj.getClass())) { + this.insertByKey((K) obj); + } else { + throw new InvalidIndexActionException("Index " + this.getIndexName() + + " cannot execute INSERT for class: " + obj.getClass()); + } + } + + @Override + public void insertByDto(E item) { + try { + XContentBuilder doc = normalizer.normalize(item); + String keyValue = getKeyValue(item.getKey()); + if (doc != null && keyValue != null && !keyValue.isEmpty()) { + insertDocument(doc, keyValue); + } else { + LOG.error("Could not normalize document {} for insert in {}", + keyValue, getIndexName()); + } + } catch (Exception e) { + LOG.error("Could not update document for index {}: {}", + getIndexName(), e.getMessage()); + } + } + @Override - public void insert(K key) { + public void insertByKey(K key) { try { XContentBuilder doc = normalizer.normalize(key); String keyValue = getKeyValue(key); if (doc != null && keyValue != null && !keyValue.isEmpty()) { - LOG.debug("Insert document with key {}", key); - getClient().index( - new IndexRequest(this.getIndexName(), - getType(), keyValue) - //.refresh(true) - .source(doc) - ).get(); + insertDocument(doc, keyValue); } else { LOG.error("Could not normalize document {} for insert in {}", key, getIndexName()); @@ -147,31 +181,89 @@ public abstract class BaseIndex<K extends Serializable, E extends Dto<K>> implem } } + private void updateDocument(XContentBuilder doc, String key) throws ExecutionException, InterruptedException { + LOG.debug("UPDATE _id:{} in index {}", key, this.getIndexName()); + getClient().update( + new UpdateRequest(this.getIndexName(), + this.getType(), key) + //.refresh(true) + .doc(doc) + ).get(); + } + + @Override + public void update(Object obj) throws InvalidIndexActionException { + if(Dto.class.isAssignableFrom(obj.getClass())){ + this.updateByDto((E) obj); + } else if(Serializable.class.isAssignableFrom(obj.getClass())) { + this.updateByKey((K) obj); + } else { + throw new InvalidIndexActionException("Index " + this.getIndexName() + + " cannot execute UPDATE for class: " + obj.getClass()); + } + } + @Override - public void update(K key) { + public void updateByDto(E item) { try { - LOG.info("Update document with key {}", key); - XContentBuilder doc = normalizer.normalize(key); - getClient().update( - new UpdateRequest(this.getIndexName(), - this.getType(), this.getKeyValue(key)) - //.refresh(true) - .doc(doc) - ).get(); + XContentBuilder doc = normalizer.normalize(item); + String keyValue = getKeyValue(item.getKey()); + this.updateDocument(doc, keyValue); } catch (Exception e) { - LOG.error("Could not update documet for index {}: {}", + LOG.error("Could not update document for index {}: {}", this.getIndexName(), e.getMessage()); } } @Override - public void delete(K key) { - LOG.info("Deleting document with key {}", key); + public void updateByKey(K key) { + try { + XContentBuilder doc = normalizer.normalize(key); + String keyValue = getKeyValue(key); + this.updateDocument(doc, keyValue); + } catch (Exception e) { + LOG.error("Could not update document for index {}: {}", + this.getIndexName(), e.getMessage()); + } + } + + private void deleteDocument(String key) throws ExecutionException, InterruptedException { + LOG.debug("DELETE _id:{} in index {}", key, this.getIndexName()); getClient() - .prepareDelete(this.getIndexName(), this.getType(), this.getKeyValue(key)) + .prepareDelete(this.getIndexName(), this.getType(), key) .get(); } + @Override + public void delete(Object obj) throws InvalidIndexActionException { + if(Dto.class.isAssignableFrom(obj.getClass())){ + this.deleteByDto((E) obj); + } else if(Serializable.class.isAssignableFrom(obj.getClass())) { + this.deleteByKey((K) obj); + } else { + throw new InvalidIndexActionException("Index " + this.getIndexName() + + " cannot execute DELETE for class: " + obj.getClass()); + } + } + + @Override + public void deleteByKey(K key) { + try { + this.deleteDocument(this.getKeyValue(key)); + } catch (Exception e) { + LOG.error("Could not DELETE _id:{} for index {}: {}", + this.getKeyValue(key), this.getIndexName(), e.getMessage()); } + } + + @Override + public void deleteByDto(E item) { + try { + this.deleteDocument(this.getKeyValue(item.getKey())); + } catch (Exception e) { + LOG.error("Could not DELETE _id:{} for index {}: {}", + this.getKeyValue(item.getKey()), this.getIndexName(), e.getMessage()); } + } + /* Synchronization methods */ Long lastSynch = 0l; diff --git a/sonar-server/src/main/java/org/sonar/server/search/Index.java b/sonar-server/src/main/java/org/sonar/server/search/Index.java index e41e2767b3e..826d56ccc54 100644 --- a/sonar-server/src/main/java/org/sonar/server/search/Index.java +++ b/sonar-server/src/main/java/org/sonar/server/search/Index.java @@ -20,28 +20,42 @@ package org.sonar.server.search; import org.picocontainer.Startable; +import org.sonar.core.db.Dto; import javax.annotation.CheckForNull; import java.io.Serializable; -public interface Index<K extends Serializable> extends Startable { +public interface Index<E extends Dto<K>, K extends Serializable> extends Startable { String getIndexName(); @CheckForNull - Hit getByKey(K key); + Hit getByKey(K item); void refresh(); - void insert(K key); + void insert(Object obj) throws InvalidIndexActionException; - void update(K key); + void insertByKey(K key); - void delete(K key); + void insertByDto(E item); + + void update(Object obj) throws InvalidIndexActionException; + + void updateByKey(K key); + + void updateByDto(E item); + + void delete(Object obj) throws InvalidIndexActionException; + + void deleteByKey(K key); + + void deleteByDto(E item); Long getLastSynchronization(); void setLastSynchronization(Long time); + } diff --git a/sonar-server/src/main/java/org/sonar/server/search/IndexAction.java b/sonar-server/src/main/java/org/sonar/server/search/IndexAction.java index 0ab9744a2f3..eb81bee651d 100644 --- a/sonar-server/src/main/java/org/sonar/server/search/IndexAction.java +++ b/sonar-server/src/main/java/org/sonar/server/search/IndexAction.java @@ -22,10 +22,11 @@ package org.sonar.server.search; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.sonar.core.cluster.QueueAction; +import org.sonar.core.db.Dto; import java.io.Serializable; -public class IndexAction<K extends Serializable> extends QueueAction { +public class IndexAction extends QueueAction { private static final Logger LOG = LoggerFactory.getLogger(IndexAction.class); @@ -35,16 +36,16 @@ public class IndexAction<K extends Serializable> extends QueueAction { } private String indexName; - private K key; + private Object item; private Method method; - private Index<K> index; + private Index index; - public IndexAction(String indexName, Method method, K key){ + public IndexAction(String indexName, Method method, Object item){ super(); this.indexName = indexName; this.method = method; - this.key = key; + this.item = item; } public Method getMethod(){ @@ -59,12 +60,12 @@ public class IndexAction<K extends Serializable> extends QueueAction { this.indexName = indexName; } - public K getKey() { - return key; + public Object getItem() { + return item; } - public void setKey(K key) { - this.key = key; + public void setItem(Object item) { + this.item = item; } public void setMethod(Method method) { @@ -74,25 +75,32 @@ public class IndexAction<K extends Serializable> extends QueueAction { @Override public void doExecute() { long start = System.currentTimeMillis(); - if (this.getMethod().equals(Method.DELETE)) { - index.delete(this.getKey()); - } else if (this.getMethod().equals(Method.INSERT)) { - index.insert(this.getKey()); - } else if (this.getMethod().equals(Method.UPDATE)) { - index.update(this.getKey()); + try { + if (this.getMethod().equals(Method.DELETE)) { + index.delete(this.getItem()); + } else if (this.getMethod().equals(Method.INSERT)) { + index.insert(this.getItem()); + } else if (this.getMethod().equals(Method.UPDATE)) { + index.update(this.getItem()); + } + LOG.debug("Action {} in {} took {}ms", this.getMethod(), + this.getIndexName(), (System.currentTimeMillis() - start)); + } catch (Exception e) { + LOG.error("Index {} cannot execute {} with {}", this.getIndexName(), + this.getMethod(), this.getItem().toString()); + } catch (InvalidIndexActionException e) { + LOG.error("Index {} cannot execute {} with {}", this.getIndexName(), + this.getMethod(), this.getItem().toString()); } - //TODO execute ACtion when DTO available - LOG.debug("Action {} in {} took {}ms", this.getMethod(), - this.getIndexName(), (System.currentTimeMillis() - start)); } @SuppressWarnings("unchecked") - public void setIndex(Index<?> index) { - this.index = (Index<K>) index; + public void setIndex(Index index) { + this.index = index; } @Override public String toString(){ - return "{IndexAction {key: " + getKey()+"}"; + return "{IndexAction {key: " + getItem()+"}"; } } diff --git a/sonar-server/src/main/java/org/sonar/server/search/IndexSynchronizer.java b/sonar-server/src/main/java/org/sonar/server/search/IndexSynchronizer.java index 56ae2f4b8e9..6b8cd0565af 100644 --- a/sonar-server/src/main/java/org/sonar/server/search/IndexSynchronizer.java +++ b/sonar-server/src/main/java/org/sonar/server/search/IndexSynchronizer.java @@ -30,11 +30,11 @@ public class IndexSynchronizer<K extends Serializable> { private static final Logger LOG = LoggerFactory.getLogger(IndexSynchronizer.class); - private final Index<K> index; + private final Index index; private final Dao<?,K> dao; private final WorkQueue workQueue; - public IndexSynchronizer(Index<K> index, Dao<?,K> dao, WorkQueue workQueue) { + public IndexSynchronizer(Index index, Dao<?,K> dao, WorkQueue workQueue) { this.index = index; this.dao = dao; this.workQueue = workQueue; @@ -51,7 +51,7 @@ public class IndexSynchronizer<K extends Serializable> { if (LOG.isTraceEnabled()) { LOG.trace("Adding {} to workQueue for {}", key, index.getClass().getSimpleName()); } - workQueue.enqueue(new IndexAction<K>(index.getIndexName(), IndexAction.Method.INSERT, key)); + workQueue.enqueue(new IndexAction(index.getIndexName(), IndexAction.Method.INSERT, key)); } return this; diff --git a/sonar-server/src/main/java/org/sonar/server/search/InvalidIndexActionException.java b/sonar-server/src/main/java/org/sonar/server/search/InvalidIndexActionException.java new file mode 100644 index 00000000000..5434fabebbb --- /dev/null +++ b/sonar-server/src/main/java/org/sonar/server/search/InvalidIndexActionException.java @@ -0,0 +1,12 @@ +package org.sonar.server.search; + +/** + * Created by gamars on 01/05/14. + * + * @since 4.4 + */ +public class InvalidIndexActionException extends Throwable { + public InvalidIndexActionException(String message) { + super(message); + } +} |