Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
jialinsun committed Mar 11, 2015
1 parent 2d1e6ec commit 377437f
Show file tree
Hide file tree
Showing 17 changed files with 380 additions and 163 deletions.
4 changes: 2 additions & 2 deletions cat-home/src/main/java/com/dianping/cat/CatHomeModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
import com.dianping.cat.report.alert.exception.FrontEndExceptionAlert;
import com.dianping.cat.report.alert.heartbeat.HeartbeatAlert;
import com.dianping.cat.report.alert.network.NetworkAlert;
import com.dianping.cat.report.alert.storage.StorageDatabaseAlert;
import com.dianping.cat.report.alert.storage.StorageSQLAlert;
import com.dianping.cat.report.alert.system.SystemAlert;
import com.dianping.cat.report.alert.thirdParty.ThirdPartyAlert;
import com.dianping.cat.report.alert.thirdParty.ThirdPartyAlertBuilder;
Expand Down Expand Up @@ -58,7 +58,7 @@ protected void execute(ModuleContext ctx) throws Exception {
AppAlert appAlert = ctx.lookup(AppAlert.class);
WebAlert webAlert = ctx.lookup(WebAlert.class);
TransactionAlert transactionAlert = ctx.lookup(TransactionAlert.class);
StorageDatabaseAlert storageDatabaseAlert = ctx.lookup(StorageDatabaseAlert.class);
StorageSQLAlert storageDatabaseAlert = ctx.lookup(StorageSQLAlert.class);

Threads.forGroup("cat").start(networkAlert);
Threads.forGroup("cat").start(databaseAlert);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@
import com.dianping.cat.report.alert.sender.spliter.SpliterManager;
import com.dianping.cat.report.alert.sender.spliter.WeixinSpliter;
import com.dianping.cat.report.alert.service.AlertEntityService;
import com.dianping.cat.report.alert.storage.StorageDatabaseAlert;
import com.dianping.cat.report.alert.storage.StorageSQLAlert;
import com.dianping.cat.report.alert.summary.AlertSummaryExecutor;
import com.dianping.cat.report.alert.summary.AlertSummaryService;
import com.dianping.cat.report.alert.summary.build.AlertInfoBuilder;
Expand Down Expand Up @@ -245,7 +245,7 @@ public List<Component> defineComponents() {
all.add(C(TransactionAlert.class).req(TransactionMergeHelper.class, DataChecker.class, AlertManager.class)
.req(ModelService.class, TransactionAnalyzer.ID).req(TransactionRuleConfigManager.class));

all.add(C(StorageDatabaseAlert.class).req(StorageMergeHelper.class, DataChecker.class, AlertManager.class)
all.add(C(StorageSQLAlert.class).req(StorageMergeHelper.class, DataChecker.class, AlertManager.class)
.req(ModelService.class, StorageAnalyzer.ID)
.req(StorageSQLRuleConfigManager.class, StorageGroupConfigManager.class, StorageGraphBuilder.class));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@
import com.dianping.cat.home.rule.entity.Condition;
import com.dianping.cat.home.rule.entity.Config;
import com.dianping.cat.home.rule.entity.Rule;
import com.dianping.cat.home.storage.entity.Storage;
import com.dianping.cat.home.storage.entity.StorageGroup;
import com.dianping.cat.message.Transaction;
import com.dianping.cat.report.alert.AlertResultEntity;
import com.dianping.cat.report.alert.DataChecker;
Expand Down Expand Up @@ -210,14 +208,32 @@ private void processRule(Rule rule, String name, String ip, StorageReport report
private void processStorage(String id) {
StorageReport currentReport = fetchStorageReport(id, ModelPeriod.CURRENT);

for (String ip : currentReport.getIps()) {
List<Rule> rules = getRuleConfigManager().findRule(id, ip);
if (currentReport != null) {
for (String ip : currentReport.getIps()) {
List<Rule> rules = getRuleConfigManager().findRule(id, ip);

for (Rule rule : rules) {
processRule(rule, id, ip, currentReport);
for (Rule rule : rules) {
processRule(rule, id, ip, currentReport);
}
}
}
}

private List<String> queryCurrentStorages() {
List<String> ids = new ArrayList<String>(m_storageConfigManager.queryStorageGroup(getType()).getStorages()
.keySet());
ModelRequest request = new ModelRequest("*-" + getType(), ModelPeriod.CURRENT.getStartTime()) //
.setProperty("ip", Constants.ALL);
ModelResponse<StorageReport> response = m_service.invoke(request);

if (response != null) {
StorageReport report = response.getModel();

if (report != null) {
ids.addAll(report.getIds());
}
}
return ids;
}

@Override
Expand All @@ -229,11 +245,11 @@ public void run() {
long current = System.currentTimeMillis();

try {
StorageGroup groups = m_storageConfigManager.queryStorageGroup(StorageConstants.SQL_TYPE);
List<String> storages = queryCurrentStorages();

for (Entry<String, Storage> entry : groups.getStorages().entrySet()) {
for (String storage : storages) {
try {
processStorage(entry.getValue().getId());
processStorage(storage);
} catch (Exception e) {
Cat.logError(e);
}
Expand All @@ -242,6 +258,7 @@ public void run() {
t.setStatus(Transaction.SUCCESS);
} catch (Exception e) {
t.setStatus(e);
Cat.logError(e);
} finally {
t.complete();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import com.dianping.cat.system.config.StorageSQLRuleConfigManager;
import com.dianping.cat.system.config.StorageRuleConfigManager;

public class StorageDatabaseAlert extends AbstractStorageAlert {
public class StorageSQLAlert extends AbstractStorageAlert {

@Inject
private StorageSQLRuleConfigManager m_configManager;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

import org.unidal.lookup.annotation.Inject;
import org.unidal.lookup.util.StringUtils;
import org.unidal.tuple.Pair;
import org.unidal.web.mvc.PageHandler;
import org.unidal.web.mvc.annotation.InboundActionMeta;
import org.unidal.web.mvc.annotation.OutboundActionMeta;
Expand All @@ -21,6 +22,7 @@
import com.dianping.cat.consumer.storage.StorageAnalyzer;
import com.dianping.cat.consumer.storage.model.entity.StorageReport;
import com.dianping.cat.helper.JsonBuilder;
import com.dianping.cat.helper.SortHelper;
import com.dianping.cat.helper.TimeHelper;
import com.dianping.cat.home.storage.alert.entity.StorageAlertInfo;
import com.dianping.cat.report.ReportPage;
Expand All @@ -32,6 +34,7 @@
import com.dianping.cat.service.ModelRequest;
import com.dianping.cat.service.ModelResponse;
import com.dianping.cat.system.config.StorageGroupConfigManager;
import com.dianping.cat.system.config.StorageGroupConfigManager.Department;

public class Handler implements PageHandler<Context> {
@Inject
Expand Down Expand Up @@ -71,54 +74,68 @@ private void buildLineCharts(Model model, Payload payload, String ipAddress, Sto
model.setLongTrend(m_jsonBuilder.toJson(lineCharts.get(StorageConstants.LONG)));
}

private Set<String> buildOperations(Payload payload, Model model) {
private String buildOperationStr(List<String> ops) {
return StringUtils.join(ops, ";");
}

private Pair<Boolean, Set<String>> buildOperations(Payload payload, Model model, Set<String> defaultValue) {
String operations = payload.getOperations();
Set<String> ops = new HashSet<String>();
boolean filter = false;

if (StringUtils.isNotEmpty(operations)) {
String[] op = operations.split(";");
if (operations != null) {
if (operations.length() > 0) {
filter = true;
String[] op = operations.split(";");

for (int i = 0; i < op.length; i++) {
ops.add(op[i]);
for (int i = 0; i < op.length; i++) {
ops.add(op[i]);
}
} else {
ops.addAll(defaultValue);
}
} else {
String type = payload.getType();
List<String> defaultMethods = new ArrayList<String>();

if (StorageConstants.CACHE_TYPE.equals(type)) {
defaultMethods = StorageConstants.CACHE_METHODS;
} else if (StorageConstants.SQL_TYPE.equals(type)) {
defaultMethods = StorageConstants.SQL_METHODS;
}

for (String method : defaultMethods) {
if (defaultValue.contains(method)) {
ops.add(method);
}
}
payload.setOperations(buildOperationStr(defaultMethods));
}
return ops;
return new Pair<Boolean, Set<String>>(filter, ops);
}

private StorageReport buildReport(Payload payload, Model model, StorageReport storageReport) {
Set<String> ops = buildOperations(payload, model);

if (storageReport != null) {
Pair<Boolean, Set<String>> pair = buildOperations(payload, model, storageReport.getOps());
storageReport = m_mergeHelper.mergeReport(storageReport, payload.getIpAddress(), Constants.ALL);
Set<String> ops = pair.getValue();

if (ops.size() > 0) {
if (pair.getKey()) {
StorageOperationFilter filter = new StorageOperationFilter(ops);
filter.visitStorageReport(storageReport);

storageReport = filter.getStorageReport();
} else {
String type = payload.getType();
Set<String> reportOps = storageReport.getOps();

if (StorageConstants.SQL_TYPE.equals(type)) {
ops = reportOps;
} else if (StorageConstants.CACHE_TYPE.equals(type)) {

for (String method : StorageConstants.CACHE_METHODS) {
if (reportOps.contains(method)) {
ops.add(method);
}
}
}
}

StorageSorter sorter = new StorageSorter(storageReport, payload.getSort());
storageReport = sorter.getSortedReport();

model.setReport(storageReport);
}
model.setOperations(ops);

model.setOperations(ops);
Map<String, Department> departments = m_storageGroupConfigManager.queryStorageDepartments(
SortHelper.sortDomain(storageReport.getIds()), payload.getType());
model.setDepartments(departments);
}
return storageReport;
}

Expand Down Expand Up @@ -166,6 +183,7 @@ public void handleOutbound(Context ctx) throws ServletException, IOException {
model.setReportEnd(new Date(payload.getDate() + TimeHelper.ONE_HOUR - 1));
break;
}

model.setPage(ReportPage.STORAGE);

if (!ctx.isProcessStopped()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,18 @@
import java.util.Map;
import java.util.Set;

import org.unidal.lookup.ContainerLoader;

import com.dianping.cat.Cat;
import com.dianping.cat.consumer.storage.model.entity.Machine;
import com.dianping.cat.consumer.storage.model.entity.StorageReport;
import com.dianping.cat.helper.SortHelper;
import com.dianping.cat.home.storage.alert.entity.StorageAlertInfo;
import com.dianping.cat.report.page.AbstractReportModel;
import com.dianping.cat.system.config.StorageGroupConfigManager;
import com.dianping.cat.system.config.StorageGroupConfigManager.Department;

public class Model extends AbstractReportModel<Action, Context> {

private StorageReport m_report;

private Set<String> m_operations;
private Set<String> m_operations = new HashSet<String>();

private String m_countTrend;

Expand All @@ -46,15 +42,10 @@ public class Model extends AbstractReportModel<Action, Context> {

private Map<String, StorageAlertInfo> m_alertInfos;

private StorageGroupConfigManager m_configManager;
private Map<String, Department> m_departments;

public Model(Context ctx) {
super(ctx);
try {
m_configManager = ContainerLoader.getDefaultContainer().lookup(StorageGroupConfigManager.class);
} catch (Exception e) {
Cat.logError(e);
}
}

public Map<String, StorageAlertInfo> getAlertInfos() {
Expand Down Expand Up @@ -86,7 +77,7 @@ public Action getDefaultAction() {
}

public Map<String, Department> getDepartments() {
return m_configManager.queryStorageDepartments(SortHelper.sortDomain(m_report.getIds()));
return m_departments;
}

@Override
Expand Down Expand Up @@ -171,6 +162,10 @@ public void setCountTrend(String countTrend) {
m_countTrend = countTrend;
}

public void setDepartments(Map<String, Department> departments) {
m_departments = departments;
}

public void setErrorTrend(String errorTrend) {
m_errorTrend = errorTrend;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,10 @@ public class Payload extends AbstractReportPayload<Action> {
private int m_frequency = 10;

@FieldMeta("count")
private int m_minuteCounts = StorageConstants.MINUTE_COUNT;
private int m_minuteCounts = StorageConstants.DEFAULT_MINUTE_COUNT;

@FieldMeta("tops")
private int m_topCounts = StorageConstants.TOP_COUNT;
private int m_topCounts = StorageConstants.DEFAULT_TOP_COUNT;

@FieldMeta("id")
private String m_id = Constants.CAT;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,12 @@ public class StorageConstants {

public static final List<String> TITLES = Arrays.asList(COUNT, AVG, ERROR, LONG);

public static final int MINUTE_COUNT = 8;
public static final int DEFAULT_MINUTE_COUNT = 8;

public static final int TOP_COUNT = 10;
public static final int DEFAULT_TOP_COUNT = 10;

public static final List<String> CACHE_METHODS = Arrays.asList("add", "get", "mGet", "remove");

public static final List<String> SQL_METHODS = Arrays.asList("select", "delete", "insert", "update");

}
Loading

0 comments on commit 377437f

Please sign in to comment.