Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
134 changes: 134 additions & 0 deletions core/src/main/java/com/cloud/agent/api/HostStatsEntryBase.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
//
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF 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.
//

package com.cloud.agent.api;

import com.cloud.host.HostStats;

/**
* Serializable host-stats payload persisted as JSON in {@code host_stats.host_stats_data}. Unlike
* {@link HostStatsEntry} it carries no {@code HostVO}, so serialization does not pull in a host entity.
*/
public class HostStatsEntryBase implements HostStats {

private long hostId;
private String entityType;
private double cpuUtilization;
private double averageLoad;
private double networkReadKBs;
private double networkWriteKBs;
private double totalMemoryKBs;
private double freeMemoryKBs;

public HostStatsEntryBase() {
}

public HostStatsEntryBase(long hostId, String entityType, double cpuUtilization, double averageLoad,
double networkReadKBs, double networkWriteKBs, double totalMemoryKBs, double freeMemoryKBs) {
this.hostId = hostId;
this.entityType = entityType;
this.cpuUtilization = cpuUtilization;
this.averageLoad = averageLoad;
this.networkReadKBs = networkReadKBs;
this.networkWriteKBs = networkWriteKBs;
this.totalMemoryKBs = totalMemoryKBs;
this.freeMemoryKBs = freeMemoryKBs;
}

public long getHostId() {
return hostId;
}

public void setHostId(long hostId) {
this.hostId = hostId;
}

@Override
public String getEntityType() {
return entityType;
}

public void setEntityType(String entityType) {
this.entityType = entityType;
}

@Override
public double getCpuUtilization() {
return cpuUtilization;
}

public void setCpuUtilization(double cpuUtilization) {
this.cpuUtilization = cpuUtilization;
}

@Override
public double getLoadAverage() {
return averageLoad;
}

public void setAverageLoad(double averageLoad) {
this.averageLoad = averageLoad;
}

@Override
public double getNetworkReadKBs() {
return networkReadKBs;
}

public void setNetworkReadKBs(double networkReadKBs) {
this.networkReadKBs = networkReadKBs;
}

@Override
public double getNetworkWriteKBs() {
return networkWriteKBs;
}

public void setNetworkWriteKBs(double networkWriteKBs) {
this.networkWriteKBs = networkWriteKBs;
}

@Override
public double getTotalMemoryKBs() {
return totalMemoryKBs;
}

public void setTotalMemoryKBs(double totalMemoryKBs) {
this.totalMemoryKBs = totalMemoryKBs;
}

@Override
public double getFreeMemoryKBs() {
return freeMemoryKBs;
}

public void setFreeMemoryKBs(double freeMemoryKBs) {
this.freeMemoryKBs = freeMemoryKBs;
}

@Override
public double getUsedMemory() {
return (totalMemoryKBs - freeMemoryKBs) * 1024;
}

@Override
public HostStats getHostStats() {
return this;
}
}
88 changes: 88 additions & 0 deletions engine/schema/src/main/java/com/cloud/host/HostStatsVO.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF 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.
package com.cloud.host;

import java.util.Date;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;

import org.apache.cloudstack.utils.reflectiontostringbuilderutils.ReflectionToStringBuilderUtils;

/** One persisted historical host-stats sample. */
@Entity
@Table(name = "host_stats")
public class HostStatsVO {

@Id
@Column(name = "id", updatable = false, nullable = false)
protected long id;

@Column(name = "host_id", updatable = false, nullable = false)
protected Long hostId;

@Column(name = "mgmt_server_id", updatable = false, nullable = false)
protected Long mgmtServerId;

@Column(name = "timestamp", updatable = false)
@Temporal(value = TemporalType.TIMESTAMP)
protected Date timestamp;

@Column(name = "host_stats_data", updatable = false, nullable = false, length = 65535)
protected String hostStatsData;

public HostStatsVO(Long hostId, Long mgmtServerId, Date timestamp, String hostStatsData) {
this.hostId = hostId;
this.mgmtServerId = mgmtServerId;
this.timestamp = timestamp;
this.hostStatsData = hostStatsData;
}

public HostStatsVO() {

}

public long getId() {
return id;
}

public Long getHostId() {
return hostId;
}

public Long getMgmtServerId() {
return mgmtServerId;
}

public Date getTimestamp() {
return timestamp;
}

public String getHostStatsData() {
return hostStatsData;
}

@Override
public String toString() {
return ReflectionToStringBuilderUtils.reflectOnlySelectedFields(this, "hostId", "mgmtServerId", "timestamp", "hostStatsData");
}

}
42 changes: 42 additions & 0 deletions engine/schema/src/main/java/com/cloud/host/dao/HostStatsDao.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF 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.
package com.cloud.host.dao;

import java.util.Date;
import java.util.List;

import com.cloud.host.HostStatsVO;
import com.cloud.utils.db.GenericDao;

/** DAO for the host_stats table. */
public interface HostStatsDao extends GenericDao<HostStatsVO, Long> {

List<HostStatsVO> findByHostId(long hostId);

List<HostStatsVO> findByHostIdAndTimestampGreaterThanEqual(long hostId, Date time);

List<HostStatsVO> findByHostIdAndTimestampLessThanEqual(long hostId, Date time);

List<HostStatsVO> findByHostIdAndTimestampBetween(long hostId, Date startTime, Date endTime);

/**
* Expunges all host stats older than {@code limitDate}.
* @param limitPerQuery max rows removed per query; 0 or negative means no limit.
*/
void removeAllByTimestampLessThan(Date limitDate, long limitPerQuery);

}
115 changes: 115 additions & 0 deletions engine/schema/src/main/java/com/cloud/host/dao/HostStatsDaoImpl.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF 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.
package com.cloud.host.dao;

import java.util.Date;
import java.util.List;

import javax.annotation.PostConstruct;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.stereotype.Component;

import com.cloud.host.HostStatsVO;
import com.cloud.utils.db.GenericDaoBase;
import com.cloud.utils.db.SearchBuilder;
import com.cloud.utils.db.SearchCriteria;
import com.cloud.utils.db.SearchCriteria.Op;

/** DAO for the host_stats table. */
@Component
public class HostStatsDaoImpl extends GenericDaoBase<HostStatsVO, Long> implements HostStatsDao {

protected Logger logger = LogManager.getLogger(getClass());

protected SearchBuilder<HostStatsVO> hostIdSearch;
protected SearchBuilder<HostStatsVO> hostIdTimestampGreaterThanEqualSearch;
protected SearchBuilder<HostStatsVO> hostIdTimestampLessThanEqualSearch;
protected SearchBuilder<HostStatsVO> hostIdTimestampBetweenSearch;
protected SearchBuilder<HostStatsVO> timestampSearch;

@PostConstruct
protected void init() {
hostIdSearch = createSearchBuilder();
hostIdSearch.and("hostId", hostIdSearch.entity().getHostId(), Op.EQ);
hostIdSearch.done();

hostIdTimestampGreaterThanEqualSearch = createSearchBuilder();
hostIdTimestampGreaterThanEqualSearch.and("hostId", hostIdTimestampGreaterThanEqualSearch.entity().getHostId(), Op.EQ);
hostIdTimestampGreaterThanEqualSearch.and("timestamp", hostIdTimestampGreaterThanEqualSearch.entity().getTimestamp(), Op.GTEQ);
hostIdTimestampGreaterThanEqualSearch.done();

hostIdTimestampLessThanEqualSearch = createSearchBuilder();
hostIdTimestampLessThanEqualSearch.and("hostId", hostIdTimestampLessThanEqualSearch.entity().getHostId(), Op.EQ);
hostIdTimestampLessThanEqualSearch.and("timestamp", hostIdTimestampLessThanEqualSearch.entity().getTimestamp(), Op.LTEQ);
hostIdTimestampLessThanEqualSearch.done();

hostIdTimestampBetweenSearch = createSearchBuilder();
hostIdTimestampBetweenSearch.and("hostId", hostIdTimestampBetweenSearch.entity().getHostId(), Op.EQ);
hostIdTimestampBetweenSearch.and("timestamp", hostIdTimestampBetweenSearch.entity().getTimestamp(), Op.BETWEEN);
hostIdTimestampBetweenSearch.done();

timestampSearch = createSearchBuilder();
timestampSearch.and("timestamp", timestampSearch.entity().getTimestamp(), Op.LT);
timestampSearch.done();
}

@Override
public List<HostStatsVO> findByHostId(long hostId) {
SearchCriteria<HostStatsVO> sc = hostIdSearch.create();
sc.setParameters("hostId", hostId);
return listBy(sc);
}

@Override
public List<HostStatsVO> findByHostIdAndTimestampGreaterThanEqual(long hostId, Date time) {
SearchCriteria<HostStatsVO> sc = hostIdTimestampGreaterThanEqualSearch.create();
sc.setParameters("hostId", hostId);
sc.setParameters("timestamp", time);
return listBy(sc);
}

@Override
public List<HostStatsVO> findByHostIdAndTimestampLessThanEqual(long hostId, Date time) {
SearchCriteria<HostStatsVO> sc = hostIdTimestampLessThanEqualSearch.create();
sc.setParameters("hostId", hostId);
sc.setParameters("timestamp", time);
return listBy(sc);
}

@Override
public List<HostStatsVO> findByHostIdAndTimestampBetween(long hostId, Date startTime, Date endTime) {
SearchCriteria<HostStatsVO> sc = hostIdTimestampBetweenSearch.create();
sc.setParameters("hostId", hostId);
sc.setParameters("timestamp", startTime, endTime);
return listBy(sc);
}

@Override
public void removeAllByTimestampLessThan(Date limitDate, long limitPerQuery) {
SearchCriteria<HostStatsVO> sc = timestampSearch.create();
sc.setParameters("timestamp", limitDate);

logger.debug(String.format("Starting to remove all host_stats rows older than [%s].", limitDate));

long totalRemoved = batchExpunge(sc, limitPerQuery);

logger.info(String.format("Removed a total of [%s] host_stats rows older than [%s].", totalRemoved, limitDate));
}

}
Loading