Skip to content

Commit a98ef23

Browse files
Change-Id: Iedf186a9ec5449f88ce8ad26c6a0ea2503b27728
1 parent 286d03c commit a98ef23

1 file changed

Lines changed: 143 additions & 0 deletions

File tree

Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
1+
package com.dtdsoftware.splunk.logging;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
6+
/**
7+
* Common base class for all Splunk Input types.
8+
* Currently just has shared logic for queuing up events.
9+
*
10+
* @author Damien Dallimore damien@dtdsoftware.com
11+
*
12+
*/
13+
public abstract class SplunkInput {
14+
15+
//data size multipliers
16+
private static final int KB = 1024;
17+
private static final int MB = KB*1024;
18+
private static final int GB = MB*1024;
19+
20+
//default to 500K
21+
private long maxQueueSize = 500*KB;
22+
//default. If true,queue will get emptied when it fills up to accommodate new data.
23+
private boolean dropEventsOnQueueFull = false;
24+
25+
//Using this collection structure to implement the FIFO queue
26+
private List <String>queue = new ArrayList<String>();
27+
28+
private long currentQueueSizeInBytes = 0;
29+
30+
/**
31+
* Add an event to the tail of the FIFO queue subject to there being capacity
32+
* @param event
33+
*/
34+
protected void enqueue(String event){
35+
36+
long eventSize = event.getBytes().length;
37+
38+
if(queueHasCapacity(eventSize)){
39+
queue.add(event);
40+
currentQueueSizeInBytes += eventSize;
41+
}
42+
else if(dropEventsOnQueueFull){
43+
queue.clear();
44+
queue.add(event);
45+
currentQueueSizeInBytes = eventSize;
46+
47+
}
48+
else{
49+
//bummer , queue is full up
50+
51+
}
52+
}
53+
54+
/**
55+
* True if the queue has capacity for adding an event of the given size
56+
* @param eventSize
57+
* @return
58+
*/
59+
private boolean queueHasCapacity(long eventSize) {
60+
61+
return (currentQueueSizeInBytes+eventSize) <= maxQueueSize;
62+
}
63+
64+
/**
65+
* True if there are pending events in the queue
66+
* @return
67+
*/
68+
protected boolean queueContainsEvents(){
69+
return !queue.isEmpty();
70+
}
71+
72+
/**
73+
* Remove an event from the head of the FIFO queue or null if there are no items in the queue
74+
* @return
75+
*/
76+
protected String dequeue(){
77+
78+
if(queueContainsEvents()){
79+
String event = queue.remove(0);
80+
currentQueueSizeInBytes -= event.getBytes().length;
81+
if(currentQueueSizeInBytes < 0){
82+
currentQueueSizeInBytes = 0;
83+
}
84+
return event;
85+
}
86+
return null;
87+
}
88+
89+
/**
90+
* Set the queue size from the configured property String value.
91+
* If parsing fails , the default of 500KB will be used.
92+
*
93+
* @param rawProperty in format [<integer>|<integer>[KB|MB|GB]]
94+
*/
95+
public void setMaxQueueSize(String rawProperty) {
96+
97+
int multiplier;
98+
int factor;
99+
100+
if(rawProperty.startsWith("KB")){
101+
multiplier = KB;
102+
}
103+
else if(rawProperty.startsWith("MB")){
104+
multiplier = MB;
105+
}
106+
else if(rawProperty.startsWith("GB")){
107+
multiplier = GB;
108+
}
109+
else{
110+
return;
111+
}
112+
try {
113+
factor = Integer.parseInt(rawProperty.substring(2));
114+
} catch (NumberFormatException e) {
115+
return;
116+
}
117+
setMaxQueueSize(factor*multiplier);
118+
119+
}
120+
121+
public long getMaxQueueSize() {
122+
return maxQueueSize;
123+
}
124+
/**
125+
* Max queue size in bytes
126+
* @param maxQueueSize
127+
*/
128+
public void setMaxQueueSize(long maxQueueSize) {
129+
this.maxQueueSize = maxQueueSize;
130+
}
131+
public boolean isDropEventsOnQueueFull() {
132+
return dropEventsOnQueueFull;
133+
}
134+
/**
135+
* If true,queue will get emptied when it fills up to accommodate new data.
136+
* @param dropEventsOnQueueFull
137+
*/
138+
public void setDropEventsOnQueueFull(boolean dropEventsOnQueueFull) {
139+
this.dropEventsOnQueueFull = dropEventsOnQueueFull;
140+
}
141+
142+
143+
}

0 commit comments

Comments
 (0)