Skip to content

Commit

Permalink
merged
Browse files Browse the repository at this point in the history
  • Loading branch information
jodok committed Jan 17, 2014
2 parents 8dd9bbd + 4c0d9c6 commit 20bce20
Show file tree
Hide file tree
Showing 10 changed files with 697 additions and 28 deletions.
7 changes: 7 additions & 0 deletions sql/src/main/java/io/crate/metadata/sys/SystemReferences.java
Original file line number Diff line number Diff line change
Expand Up @@ -57,4 +57,11 @@ public static ReferenceInfo registerNodeReference(String column, DataType type,
type));
}

public static ReferenceInfo registerNodeReference(String column, DataType type) {
return register(new ReferenceInfo(
new ReferenceIdent(NODES_IDENT, column),
RowGranularity.NODE,
type));
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
/*
* Licensed to CRATE Technology GmbH ("Crate") under one or more contributor
* license agreements. See the NOTICE file distributed with this work for
* additional information regarding copyright ownership. Crate 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.
*
* However, if you have executed any another commercial license agreement
* with Crate these terms will supersede the license and you may use the
* software solely pursuant to the terms of the relevant commercial agreement.
*/

package io.crate.operator.reference.sys;

import com.google.common.collect.ImmutableList;
import io.crate.metadata.ReferenceInfo;
import io.crate.metadata.sys.SysExpression;
import io.crate.metadata.sys.SystemReferences;
import org.cratedb.DataType;
import org.elasticsearch.common.inject.Inject;
import org.elasticsearch.monitor.fs.FsStats;
import org.elasticsearch.node.service.NodeService;

public class NodeFsExpression extends SysObjectReference<Object> {

abstract class FsExpression extends SysExpression<Object> {

private final ReferenceInfo info;

FsExpression(ReferenceInfo info) {
this.info = info;
}

@Override
public ReferenceInfo info() {
return info;
}
}

public static final String COLNAME = "fs";

public static final String TOTAL = "total";
public static final String FREE = "free";
public static final String USED = "used";
public static final String FREE_PERCENT = "free_percent";
public static final String USED_PERCENT = "used_percent";

public static final ReferenceInfo INFO_FS = SystemReferences.registerNodeReference(
COLNAME, DataType.OBJECT);
public static final ReferenceInfo INFO_FS_TOTAL = SystemReferences.registerNodeReference(
COLNAME, DataType.LONG, ImmutableList.of(TOTAL));
public static final ReferenceInfo INFO_FS_FREE = SystemReferences.registerNodeReference(
COLNAME, DataType.LONG, ImmutableList.of(FREE));
public static final ReferenceInfo INFO_FS_USED = SystemReferences.registerNodeReference(
COLNAME, DataType.LONG, ImmutableList.of(USED));
public static final ReferenceInfo INFO_FS_FREE_PERCENT = SystemReferences.registerNodeReference(
COLNAME, DataType.DOUBLE, ImmutableList.of(FREE_PERCENT));
public static final ReferenceInfo INFO_FS_USED_PERCENT = SystemReferences.registerNodeReference(
COLNAME, DataType.DOUBLE, ImmutableList.of(USED_PERCENT));

private final NodeService nodeService;

@Inject
public NodeFsExpression(NodeService nodeService) {
this.nodeService = nodeService;
addChildImplementations();
}

@Override
public ReferenceInfo info() {
return INFO_FS;
}

private void addChildImplementations() {
childImplementations.put(TOTAL, new FsExpression(INFO_FS_TOTAL) {
@Override
public Long value() {
return getTotalBytesFromAllDisks();
}
});
childImplementations.put(FREE, new FsExpression(INFO_FS_FREE) {
@Override
public Long value() {
return getFreeBytesFromAllDisks();
}
});
childImplementations.put(USED, new FsExpression(INFO_FS_USED) {
@Override
public Long value() {
return getTotalBytesFromAllDisks() - getFreeBytesFromAllDisks();
}
});
childImplementations.put(FREE_PERCENT, new FsExpression(INFO_FS_FREE_PERCENT) {
@Override
public Double value() {
return new Double((getFreeBytesFromAllDisks() / (double) getTotalBytesFromAllDisks())*100);
}
});
childImplementations.put(USED_PERCENT, new FsExpression(INFO_FS_USED_PERCENT) {
@Override
public Double value() {
Long total_bytes = getTotalBytesFromAllDisks();
Long used_bytes = total_bytes - getFreeBytesFromAllDisks();
return new Double((used_bytes / (double) total_bytes)*100);
}
});
}

private long getFreeBytesFromAllDisks() {
Long bytes = 0L;
for (FsStats.Info fsInfo : nodeService.stats().getFs()) {
bytes += fsInfo.getFree().bytes();
}
return bytes;
}

private long getTotalBytesFromAllDisks() {
Long bytes = 0L;
for (FsStats.Info fsInfo : nodeService.stats().getFs()) {
bytes += fsInfo.getTotal().bytes();
}
return bytes;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
* Licensed to CRATE Technology GmbH ("Crate") under one or more contributor
* license agreements. See the NOTICE file distributed with this work for
* additional information regarding copyright ownership. Crate 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.
*
* However, if you have executed any another commercial license agreement
* with Crate these terms will supersede the license and you may use the
* software solely pursuant to the terms of the relevant commercial agreement.
*/

package io.crate.operator.reference.sys;

import io.crate.metadata.ReferenceInfo;
import io.crate.metadata.sys.SysExpression;
import io.crate.metadata.sys.SystemReferences;
import org.cratedb.DataType;
import org.elasticsearch.common.inject.Inject;
import org.elasticsearch.node.service.NodeService;

public class NodeHostnameExpression extends SysExpression<String> {

public static final String COLNAME = "hostname";


public static final ReferenceInfo INFO_HOSTNAME = SystemReferences.registerNodeReference(
COLNAME, DataType.STRING);


private final NodeService nodeService;

@Inject
public NodeHostnameExpression(NodeService nodeService) {
this.nodeService = nodeService;
}

@Override
public String value() {
return nodeService.stats().getHostname();
}

@Override
public ReferenceInfo info() {
return INFO_HOSTNAME;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public class NodeLoadExpression extends SysObjectReference<Double> {
public static final String FIFTEEN = "15";

public static final ReferenceInfo INFO_LOAD = SystemReferences.registerNodeReference(
COLNAME, DataType.OBJECT, null);
COLNAME, DataType.OBJECT);
public static final ReferenceInfo INFO_LOAD_1 = SystemReferences.registerNodeReference(
COLNAME, DataType.DOUBLE, ImmutableList.of(ONE));
public static final ReferenceInfo INFO_LOAD_5 = SystemReferences.registerNodeReference(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
/*
* Licensed to CRATE Technology GmbH ("Crate") under one or more contributor
* license agreements. See the NOTICE file distributed with this work for
* additional information regarding copyright ownership. Crate 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.
*
* However, if you have executed any another commercial license agreement
* with Crate these terms will supersede the license and you may use the
* software solely pursuant to the terms of the relevant commercial agreement.
*/

package io.crate.operator.reference.sys;

import com.google.common.collect.ImmutableList;
import io.crate.metadata.ReferenceInfo;
import io.crate.metadata.sys.SysExpression;
import io.crate.metadata.sys.SystemReferences;
import org.cratedb.DataType;
import org.elasticsearch.common.inject.Inject;
import org.elasticsearch.monitor.os.OsService;

public class NodeMemoryExpression extends SysObjectReference<Object> {

abstract class MemoryExpression extends SysExpression<Object> {

private final ReferenceInfo info;

MemoryExpression(ReferenceInfo info) {
this.info = info;
}

@Override
public ReferenceInfo info() {
return info;
}

}

public static final String COLNAME = "mem";

public static final String FREE = "free";
public static final String USED = "used";
public static final String FREE_PERCENT = "free_percent";
public static final String USED_PERCENT = "used_percent";

public static final ReferenceInfo INFO_MEM = SystemReferences.registerNodeReference(
COLNAME, DataType.OBJECT);
public static final ReferenceInfo INFO_MEM_FREE = SystemReferences.registerNodeReference(
COLNAME, DataType.LONG, ImmutableList.of(FREE));
public static final ReferenceInfo INFO_MEM_USED = SystemReferences.registerNodeReference(
COLNAME, DataType.LONG, ImmutableList.of(USED));
public static final ReferenceInfo INFO_MEM_FREE_PERCENT = SystemReferences.registerNodeReference(
COLNAME, DataType.SHORT, ImmutableList.of(FREE_PERCENT));
public static final ReferenceInfo INFO_MEM_USED_PERCENT = SystemReferences.registerNodeReference(
COLNAME, DataType.SHORT, ImmutableList.of(USED_PERCENT));


private final OsService osService;

@Inject
public NodeMemoryExpression(OsService osService) {
this.osService = osService;
addChildImplementations();
}

@Override
public ReferenceInfo info() {
return INFO_MEM;
}

private void addChildImplementations() {
childImplementations.put(FREE, new MemoryExpression(INFO_MEM_FREE) {
@Override
public Long value() {
return osService.stats().mem().free().bytes();
}
});
childImplementations.put(USED, new MemoryExpression(INFO_MEM_USED) {
@Override
public Long value() {
return osService.stats().mem().used().bytes();
}
});
childImplementations.put(FREE_PERCENT, new MemoryExpression(INFO_MEM_FREE_PERCENT) {
@Override
public Short value() {
return osService.stats().mem().freePercent();
}
});
childImplementations.put(USED_PERCENT, new MemoryExpression(INFO_MEM_USED_PERCENT) {
@Override
public Short value() {
return osService.stats().mem().usedPercent();
}
});
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
* Licensed to CRATE Technology GmbH ("Crate") under one or more contributor
* license agreements. See the NOTICE file distributed with this work for
* additional information regarding copyright ownership. Crate 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.
*
* However, if you have executed any another commercial license agreement
* with Crate these terms will supersede the license and you may use the
* software solely pursuant to the terms of the relevant commercial agreement.
*/

package io.crate.operator.reference.sys;

import io.crate.metadata.ReferenceInfo;
import io.crate.metadata.sys.SysExpression;
import io.crate.metadata.sys.SystemReferences;
import org.cratedb.DataType;
import org.elasticsearch.common.inject.Inject;
import org.elasticsearch.node.service.NodeService;

public class NodeNameExpression extends SysExpression<String> {

public static final String COLNAME = "name";


public static final ReferenceInfo INFO_NAME = SystemReferences.registerNodeReference(
COLNAME, DataType.STRING);


private final NodeService nodeService;

@Inject
public NodeNameExpression(NodeService nodeService) {
this.nodeService = nodeService;
}

@Override
public String value() {
return nodeService.stats().getNode().getName();
}

@Override
public ReferenceInfo info() {
return INFO_NAME;
}

}
Loading

0 comments on commit 20bce20

Please sign in to comment.