Java language logging and runtime information
This example uses the following file name: TestJavaRuntime.java
Code
The code in this example explores functionality that may not be commonly used. Logging can be used to help trace the execution of an AE. To be of use, the AE must be registered with a log mask and logging must be enabled via nzudxdbg. To receive system logging messages in the window where the NPS system was started, you need to start NPS with the '-i' option (for example, 'nzstart -i'). This causes the NPS processes to remain attached to the terminal. Runtime information provides statistics about the NPS system, including the number of dataslices, the number of SPUs, and locus of execution.
import org.netezza.ae.*;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;
public class TestJavaRuntime {
private static final Executor exec =
Executors.newCachedThreadPool();
public static final void main(String [] args) {
try {
mainImpl(args);
} catch (Throwable t) {
System.err.println(t.toString());
NzaeUtil.logException(t, "main");
}
}
public static final void mainImpl(String [] args) {
NzaeApiGenerator helper = new NzaeApiGenerator();
while (true) {
final NzaeApi api = helper.getApi(NzaeApi.FUNCTION);
if (api.apiType == NzaeApi.FUNCTION) {
if (!helper.isRemote()) {
run(api.aeFunction);
break;
} else {
Runnable task = new Runnable() {
public void run() {
try {
TestJavaRuntime.run(api.aeFunction);
} finally {
api.aeFunction.close();
}
}
};
exec.execute(task);
}
}
}
helper.close();
}
public static void validateRuntime(Nzae aeFunc, long[] args, int type)
{
// argss:
// dataslice, transaction, hardware, numslices, numspus, locus
long dataslice = args[0];
long txid = args[1];
long hwid = args[2];
long nslices = args[3];
long nspus = args[4];
long locus = args[5];
NzaeRuntime aeRun = aeFunc.getRuntime();
String buf;
if (locus != aeRun.getLocus())
{
buf = "test fails: 1\n";
aeFunc.log(Nzae.LOG_DEBUG, buf);
}
if (aeRun.getSuggestedMemoryLimit() != 123)
{
buf = "test fails: 7 \n";
aeFunc.log(Nzae.LOG_DEBUG, buf);
}
if (aeRun.getUserQuery() == false)
{
buf ="test fails: 8 \n";
aeFunc.log(Nzae.LOG_DEBUG, buf);
}
if (type != aeRun.getAdapterType())
{
buf = "test fails: 9 \n";
aeFunc.log(Nzae.LOG_DEBUG, buf);
}
if (locus == NzaeRuntime.NZAE_LOCUS_POSTGRES) {
return;
}
if (dataslice != aeRun.getDataSliceId())
{
buf = "test fails: 2 \n";
aeFunc.log(Nzae.LOG_DEBUG, buf);
}
if ((txid != 0) && (txid != aeRun.getTransactionId()))
{
buf ="test fails: 3 \n";
aeFunc.log(Nzae.LOG_DEBUG, buf);
}
if (hwid != aeRun.getHardwareId())
{
buf = "test fails: 4 \n";
aeFunc.log(Nzae.LOG_DEBUG, buf);
}
if (nslices != aeRun.getNumberDataSlices())
{
buf = "test fails: 5 \n";
aeFunc.log(Nzae.LOG_DEBUG, buf);
}
if (nspus != aeRun.getNumberSpus())
{
buf = "test fails: 6 \n";
aeFunc.log(Nzae.LOG_DEBUG, buf);
}
}
public static class MyHandler implements NzaeMessageHandler
{
public void evaluate(Nzae ae, NzaeRecord input, NzaeRecord output) {
final NzaeMetadata meta = ae.getMetadata();
long ar[] = new long[6];
for (int i=0; i < 6; i++)
{
Object o = input.getField(i);
NzaeFieldInfo fi = input.getFieldInfo(i);
if (fi.getNzType() == NzaeDataTypes.NZUDSUDX_INT64){
ar[i] = (Long)o;
}
else if (fi.getNzType() == NzaeDataTypes.NZUDSUDX_INT32){
ar[i] = (Integer)o;
}
}
validateRuntime(ae, ar, NzaeRuntime.NZAE_ADAPTER_UDTF);
String str = "done";
output.setField(0,str);
}
}
public static int run(Nzae ae)
{
ae.run(new MyHandler());
return 0;
}
}
Compilation
Use the standard compile:
$NZ_EXPORT_DIR/ae/utilities/bin/compile_ae --language java --template compile \
TestJavaRuntime.java --version 3
Registration
Register the example using the --mem and --mask options. The --mask option enables logging for DEBUG and the --mem option sets the memory runtime information.
$NZ_EXPORT_DIR/ae/utilities/bin/register_ae \
--sig "runtimeae(int8,int8,int8,int8,int8,int8)" \
--return "table(val varchar(1000))" --class AeUdtf --language java \
--template udtf --version 3 --define "java_class=TestJavaRuntime" \
--mem 123 --mask debug
Running
Before running, enable logging by running nzudxdbg:
nzudxdbg
Processing 1 spus
.
done
Processing host
done
Run the query in nzsql:
SELECT * FROM TABLE WITH FINAL(runtimeae(1,1,1,1,1,1));
VAL
------
done
(1 row)
The NPS system logging appears in the window where the NPS system was started:
04-16-10 12:27:27 (dbos.19600) [d,udx ] test fails: 2
04-16-10 12:27:27 (dbos.19600) [d,udx ] test fails: 3
04-16-10 12:27:27 (dbos.19600) [d,udx ] test fails: 4
This is the expected output since the arguments specified did not match the runtime in those cases.