From: Stephane Gamard Date: Tue, 29 Apr 2014 15:34:36 +0000 (+0200) Subject: SONAR-5237 - Updated MediumTest and started on RuleService X-Git-Tag: 4.4-RC1~1341 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=680a8c7f122cfd01e147967cb78a765d827a9fb6;p=sonarqube.git SONAR-5237 - Updated MediumTest and started on RuleService --- diff --git a/sonar-server/src/main/java/org/sonar/server/rule2/RuleImpl.java b/sonar-server/src/main/java/org/sonar/server/rule2/RuleImpl.java index 03941f200ff..c096b145ec2 100644 --- a/sonar-server/src/main/java/org/sonar/server/rule2/RuleImpl.java +++ b/sonar-server/src/main/java/org/sonar/server/rule2/RuleImpl.java @@ -28,6 +28,8 @@ import java.util.List; public class RuleImpl implements Rule { + + @Override public RuleKey key() { // TODO Auto-generated method stub 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 30372871c56..6d6fd7e0036 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 @@ -20,7 +20,6 @@ package org.sonar.server.rule2; import org.sonar.core.qualityprofile.db.ActiveRuleDto; - import org.sonar.core.qualityprofile.db.ActiveRuleDao; import org.apache.commons.beanutils.BeanUtils; import org.elasticsearch.common.xcontent.XContentBuilder; @@ -92,30 +91,32 @@ public class RuleIndex extends BaseIndex { } } + private void addMatchField(XContentBuilder mapping, String field, String type) throws IOException { + mapping.startObject(field) + .field("type", type) + .field("index", "not_analyzed") + .endObject(); + } + @Override protected XContentBuilder getMapping() { try { - return jsonBuilder().startObject() + XContentBuilder mapping = jsonBuilder().startObject() .startObject(this.getType()) - .field("dynamic",true) - .startObject("properties") - .startObject("id") - .field("type", "string") - .field("index", "not_analyzed") - .endObject() - .startObject("key") - .field("type", "string") - .field("index", "not_analyzed") - .endObject() - .startObject("repositoryKey") - .field("type", "string") - .field("index", "not_analyzed") - .endObject() - .startObject("severity") - .field("type", "string") - .field("index", "not_analyzed") - .endObject() - .endObject() + .field("dynamic", true) + .startObject("properties"); + + addMatchField(mapping, "id", "string"); + addMatchField(mapping, "key", "string"); + addMatchField(mapping, "repositoryKey", "string"); + addMatchField(mapping, "severity", "string"); + + mapping.startObject("active") + .field("type", "nested") + .field("dynamic", true) + .endObject(); + + return mapping.endObject() .endObject().endObject(); // return jsonBuilder().startObject() @@ -154,41 +155,52 @@ public class RuleIndex extends BaseIndex { @Override public XContentBuilder normalize(RuleKey key) { - RuleDto rule = dao.getByKey(key); - try { + long start = System.currentTimeMillis(); + RuleDto rule = dao.getByKey(key); + LOG.info("Action dao.getByKey(key) in {} took {}ms", + this.getIndexName(), (System.currentTimeMillis() - start)); + XContentBuilder document = jsonBuilder().startObject(); Map properties = BeanUtils.describe(rule); for (Entry property : properties.entrySet()) { - LOG.info("NORMALIZING: {} -> {}",property.getKey(), property.getValue()); + LOG.trace("NORMALIZING: {} -> {}", property.getKey(), property.getValue()); document.field(property.getKey(), property.getValue()); } + start = System.currentTimeMillis(); - for(ActiveRuleDto activeRule:activeRuleDao.selectByRuleId(rule.getId())){ + document.startArray("active"); + for (ActiveRuleDto activeRule : activeRuleDao.selectByRuleId(rule.getId())) { + document.startObject(); Map activeRuleProperties = BeanUtils.describe(activeRule); for (Entry activeRuleProp : activeRuleProperties.entrySet()) { - LOG.info("NORMALIZING: --- {} -> {}",activeRuleProp.getKey(), activeRuleProp.getValue()); + LOG.trace("NORMALIZING: --- {} -> {}", activeRuleProp.getKey(), activeRuleProp.getValue()); document.field(activeRuleProp.getKey(), activeRuleProp.getValue()); } + document.endObject(); } + document.endArray(); + + LOG.info("Action activeRuleDao.selectByRuleId(rule.getId()) in {} took {}ms", + this.getIndexName(), (System.currentTimeMillis() - start)); return document.endObject(); } catch (IOException e) { - LOG.error("Could not normalize {} in {}", key, this.getClass().getSimpleName()); - e.printStackTrace(); + LOG.error("Could not normalize {} in {} because {}", + key, this.getClass().getSimpleName(), e.getMessage()); } catch (IllegalAccessException e) { - LOG.error("Could not normalize {} in {}", key, this.getClass().getSimpleName()); - e.printStackTrace(); + LOG.error("Could not normalize {} in {} because {}", + key, this.getClass().getSimpleName(), e.getMessage()); } catch (InvocationTargetException e) { - LOG.error("Could not normalize {} in {}", key, this.getClass().getSimpleName()); - e.printStackTrace(); + LOG.error("Could not normalize {} in {} because {}", + key, this.getClass().getSimpleName(), e.getMessage()); } catch (NoSuchMethodException e) { - LOG.error("Could not normalize {} in {}", key, this.getClass().getSimpleName()); - e.printStackTrace(); + LOG.error("Could not normalize {} in {} because {}", + key, this.getClass().getSimpleName(), e.getMessage()); } return null; } diff --git a/sonar-server/src/main/java/org/sonar/server/rule2/RuleService.java b/sonar-server/src/main/java/org/sonar/server/rule2/RuleService.java index dfd0835e3cf..488af9ece1f 100644 --- a/sonar-server/src/main/java/org/sonar/server/rule2/RuleService.java +++ b/sonar-server/src/main/java/org/sonar/server/rule2/RuleService.java @@ -19,6 +19,9 @@ */ package org.sonar.server.rule2; +import org.apache.commons.beanutils.BeanUtils; + +import org.sonar.api.ServerComponent; import org.sonar.api.rule.RuleKey; import org.sonar.core.rule.RuleDao; import org.sonar.core.rule.RuleDto; @@ -32,7 +35,7 @@ import java.util.Collections; /** * @since 4.4 */ -public class RuleService { +public class RuleService implements ServerComponent { private RuleDao dao; private RuleIndex index; @@ -44,10 +47,16 @@ public class RuleService { @CheckForNull public Rule getByKey(RuleKey key) { - return null; + Hit hit = index.getByKey(key); + if(hit != null){ + return toRule(hit); + } else { + return null; + } } public Collection search(RuleQuery query){ + return Collections.emptyList(); } @@ -56,6 +65,8 @@ public class RuleService { } public static Rule toRule(Hit hit){ + for() + BeanUtils.setProperty(bean, name, value); return new RuleImpl(); } } 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 41d3f732d17..727246c4689 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 @@ -25,6 +25,8 @@ import org.elasticsearch.action.delete.DeleteResponse; import org.elasticsearch.action.get.GetResponse; import org.elasticsearch.action.index.IndexRequest; import org.elasticsearch.action.index.IndexResponse; +import org.elasticsearch.action.update.UpdateRequest; +import org.elasticsearch.action.update.UpdateResponse; import org.elasticsearch.client.Client; import org.elasticsearch.common.xcontent.XContentBuilder; import org.slf4j.Logger; @@ -104,25 +106,25 @@ public abstract class BaseIndex> implem public void connect() { this.client = this.node.client(); -// /* Settings to access our local ES node */ -// Settings settings = ImmutableSettings.settingsBuilder() -// .put("client.transport.sniff", true) -// .put("cluster.name", ES_CLUSTER_NAME) -// .put("node.name", "localclient_") -// .build(); -// -// this.client = new TransportClient(settings) -// .addTransportAddress(new InetSocketTransportAddress(LOCAL_ES_NODE_HOST, LOCAL_ES_NODE_PORT)); -// -// /* -// * Cannot do that yet, need version >= 1.0 -// * ImmutableList nodes = client.connectedNodes(); -// * if (nodes.isEmpty()) { -// * throw new ElasticSearchUnavailableException("No nodes available. Verify ES is running!"); -// * } else { -// * log.info("connected to nodes: " + nodes.toString()); -// * } -// */ + // /* Settings to access our local ES node */ + // Settings settings = ImmutableSettings.settingsBuilder() + // .put("client.transport.sniff", true) + // .put("cluster.name", ES_CLUSTER_NAME) + // .put("node.name", "localclient_") + // .build(); + // + // this.client = new TransportClient(settings) + // .addTransportAddress(new InetSocketTransportAddress(LOCAL_ES_NODE_HOST, LOCAL_ES_NODE_PORT)); + // + // /* + // * Cannot do that yet, need version >= 1.0 + // * ImmutableList nodes = client.connectedNodes(); + // * if (nodes.isEmpty()) { + // * throw new ElasticSearchUnavailableException("No nodes available. Verify ES is running!"); + // * } else { + // * log.info("connected to nodes: " + nodes.toString()); + // * } + // */ } /* Cluster And ES Stats/Client methods */ @@ -136,10 +138,10 @@ public abstract class BaseIndex> implem if (!indexExistsResponse.isExists()) { - LOG.info("Setup of index {}",this.getIndexName()); + LOG.info("Setup of index {}", this.getIndexName()); try { - LOG.info("Settings: {}",getIndexSettings().string()); + LOG.info("Settings: {}", getIndexSettings().string()); LOG.info("Mapping: {}", getMapping().string()); } catch (IOException e) { // TODO Auto-generated catch block @@ -166,6 +168,8 @@ public abstract class BaseIndex> implem @Override public void executeAction(IndexAction action) { + StopWatch watch = this.createWatch(); + long start = System.currentTimeMillis(); if (action.getMethod().equals(Method.DELETE)) { this.delete(action.getKey()); } else if (action.getMethod().equals(Method.INSERT)) { @@ -173,6 +177,9 @@ public abstract class BaseIndex> implem } else if (action.getMethod().equals(Method.UPDATE)) { this.update(action.getKey()); } + LOG.info("Action {} in {} took {}ms", action.getMethod(), + this.getIndexName(), (System.currentTimeMillis() - start)); + watch.stop("Action {} in {}", action.getMethod(), this.getIndexName()); } /* Index management methods */ @@ -196,18 +203,37 @@ public abstract class BaseIndex> implem @Override public void insert(K key) { - this.update(key); + try { + XContentBuilder doc = this.normalize(key); + String keyvalue = this.getKeyValue(key); + if (doc != null && keyvalue != null && !keyvalue.isEmpty()) { + LOG.info("Update document with key {}", key); + IndexResponse result = getClient().index( + new IndexRequest(this.getIndexName(), + this.getType(), keyvalue) + .source(this.normalize(key))).get(); + } else { + LOG.error("Could not normalize document {} for insert in ", + key, this.getIndexName()); + } + } catch (Exception e) { + LOG.error("Could not update documet for index {}: {}", + this.getIndexName(), e.getMessage()); + } } @Override public void update(K key) { - LOG.info("Update document with key {}", key); - IndexResponse result = getClient().index(new IndexRequest() - .type(this.getType()) - .index(this.getIndexName()) - .id(this.getKeyValue(key)) - .source(this.normalize(key))).actionGet(); - + try { + LOG.info("Update document with key {}", key); + UpdateResponse result = getClient().update( + new UpdateRequest(this.getIndexName(), + this.getType(), this.getKeyValue(key)) + .doc(this.normalize(key))).get(); + } catch (Exception e) { + LOG.error("Could not update documet for index {}: {}", + this.getIndexName(), e.getMessage()); + } } @Override @@ -236,7 +262,7 @@ public abstract class BaseIndex> implem @Override public Long getLastSynchronization() { - // need to read that in the admin index; + // TODO need to read that in the admin index; return 0l; } } diff --git a/sonar-server/src/test/java/org/sonar/server/rule2/RuleMediumTest.java b/sonar-server/src/test/java/org/sonar/server/rule2/RuleMediumTest.java index f81399820cb..de48aaa6022 100644 --- a/sonar-server/src/test/java/org/sonar/server/rule2/RuleMediumTest.java +++ b/sonar-server/src/test/java/org/sonar/server/rule2/RuleMediumTest.java @@ -19,6 +19,7 @@ */ package org.sonar.server.rule2; +import org.junit.Before; import org.junit.Rule; import org.junit.Test; import org.sonar.api.rule.Severity; @@ -38,7 +39,18 @@ import static org.fest.assertions.Assertions.assertThat; public class RuleMediumTest { @Rule - public ServerTester tester = new ServerTester(); + public ServerTester tester = new ServerTester() + //.setProperty("sonar.log.profilingLevel", "FULL") + .setProperty("sonar.es.http.port", "8888"); + + private RuleDto dto; + private ActiveRuleDto adto; + + @Before + public void setup(){ + dto = getRuleDto(1); + adto = getActiveRuleDto(dto); + } private ActiveRuleDto getActiveRuleDto(RuleDto dto){ return new ActiveRuleDto() @@ -51,10 +63,10 @@ public class RuleMediumTest { .setSeverity(3); } - private RuleDto getRuleDto() { + private RuleDto getRuleDto(int id) { return new RuleDto() - .setId(1) - .setRuleKey("NewRuleKey") + .setId(id) + .setRuleKey("NewRuleKey="+id) .setRepositoryKey("plugin") .setName("new name") .setDescription("new description") @@ -79,17 +91,16 @@ public class RuleMediumTest { @Test public void test_dao_queue_es_search_loop() { + RuleDao dao = tester.get(RuleDao.class); ActiveRuleDao adao = tester.get(ActiveRuleDao.class); RuleIndex index = tester.get(RuleIndex.class); - RuleDto dto = getRuleDto(); dao.insert(dto); - - adao.insert(getActiveRuleDto(dto)); + adao.insert(adto); try { - Thread.sleep(100); + Thread.sleep(200); } catch (InterruptedException e) { ; } @@ -97,4 +108,18 @@ public class RuleMediumTest { Hit hit = index.getByKey(dto.getKey()); assertThat(hit.getFields().get("ruleKey")).isEqualTo(dto.getRuleKey()); } + + @Test + public void test_ruleservice_getByKey() { + RuleService service = tester.get(RuleService.class); + RuleDao dao = tester.get(RuleDao.class); + + dao.insert(dto); + + //org.sonar.server.rule2.Rule rule = service.getByKey(dto.getKey()); + + //assertThat(rule.key()).isEqualTo(dto.getKey()); + + + } }