-
Notifications
You must be signed in to change notification settings - Fork 1
/
leafheap.scala
214 lines (174 loc) · 6.91 KB
/
leafheap.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
package com.fotopedia.LeafHeap
import redis.clients.jedis.Jedis
import redis.clients.jedis.exceptions.JedisDataException
import com.fasterxml.jackson.module.scala.DefaultScalaModule
import com.fasterxml.jackson.databind.ObjectMapper
import com.fasterxml.jackson.databind.node._
import com.fasterxml.jackson.databind._
import wabisabi._
import scala.concurrent.duration._
import scala.concurrent.Await
import org.joda.time.Instant
import java.util.Calendar
import java.util.TimeZone
import java.util.GregorianCalendar
import java.lang.Thread
import java.util.concurrent.TimeoutException
import org.kohsuke.args4j.{ CmdLineParser, CmdLineException, Option => Args4jOption }
import collection.JavaConversions._
object Settings {
@Args4jOption(name = "--help", usage = "Print help")
var showHelp = false
@Args4jOption(name = "-r", usage = "Redis hostname")
var redisHostname:String = null
@Args4jOption(name = "-p", usage = "Redis port")
var redisPort = 6379
@Args4jOption(name = "-l", usage = "Redis Queues, comma separated list")
var redisQueues = "prod,testing,staging,infrabox"
@Args4jOption(name = "-e", usage = "ElasticSearch HTTP Url")
var esUrl = "http://localhost:9201/"
def es():Client = {
return new Client(esUrl)
}
@Args4jOption(name = "-d", usage = "run with debug logging parameters")
var debugMode = false
@Args4jOption(name = "--logback", usage = "overrides -d")
var logbackConfigFile:String = null
var scriptSha:String = null
}
class QueueProcessor(queueName: String) extends Runnable {
val queue = queueName
def run() {
System.out.println("Waking up QueueProcessor for " + queueName)
LeafHeap.processQueue(queue)
}
}
object LeafHeap {
def timestamp_ms(logLine:JsonNode, from:String, to:String) {
val fields = logLine.get("@fields")
val date = fields.get(from)
if (date != null) {
val jInstant = new Instant(date.longValue)
logLine.asInstanceOf[ObjectNode].set(to, new TextNode(jInstant.toString))
}
}
def rename(logLine: JsonNode, from:String, to:String) {
val fields = logLine.get("@fields")
val source = fields.asInstanceOf[ObjectNode].remove(from)
if(source != null) {
logLine.asInstanceOf[ObjectNode].set(to, source)
}
}
def processQueue(queueName: String) {
val jedis = new Jedis(Settings.redisHostname, Settings.redisPort)
val l = jedis.llen(queueName)
val prefix = "[" + queueName + "] "
System.out.println(prefix + "Queue has size:" + l )
var count = 0
var batch = scala.collection.mutable.ArrayBuffer[Object]()
var readCount = 500
while(true) {
try {
val indexName = String.format("logstash-%1$tY.%1$tm.%1$td.%1$tH", new GregorianCalendar)
val items:List[String] = try {
jedis.evalsha(Settings.scriptSha, List(queueName), List(readCount.toString)).asInstanceOf[java.util.List[String]].toList
} catch {
case e:Throwable => {
System.out.println("Reloading function (got and exception:)")
System.out.println(e.toString)
e.printStackTrace(System.out)
loadScript(jedis)
List()
}
}
items.foreach{ log_line =>
val logLineObject = mapper.readTree(log_line)
timestamp_ms(logLineObject, "date", "@timestamp")
rename(logLineObject, "instance", "host")
logLineObject.asInstanceOf[ObjectNode].set("type", new TextNode(queueName))
count = count + 1
batch += Map[String, Object]("index" -> Map[String, String]("_index" -> indexName, "_type" -> "logs"))
batch += logLineObject
}
// If redis is empty, or we have reached our maximum capacity
// ship the logs
if (count > 0) {
try {
val res = Await.result(Settings.es.bulk(data = (batch.map { v => mapper.writeValueAsString(v) }.mkString("\n"))+"\n"), Duration(8, "second")).getResponseBody
val responseObject = mapper.readTree(res)
} catch {
case e: TimeoutException => {
System.out.println(prefix + "Sending "+ count + " objects: Timeout. Throwing data out: " + e)
}
}
count = 0
batch = scala.collection.mutable.ArrayBuffer[Object]()
} else {
Thread.sleep(100)
}
} catch {
case e:Throwable => {
System.out.println("Something wrong happened:")
System.out.println(e.toString)
e.printStackTrace(System.out)
Thread.sleep(1000)
}
}
}
}
val mapper = {
val o = new ObjectMapper()
o.registerModule(DefaultScalaModule)
o
}
def loadScript(jedis:Jedis) {
Settings.scriptSha = jedis.scriptLoad("""
local i = tonumber(ARGV[1])
local res = {}
local length = redis.call('llen',KEYS[1])
if length < i then i = length end
while (i > 0) do
local item = redis.call("lpop", KEYS[1])
if (not item) then
break
end
table.insert(res, item)
i = i-1
end
return res
""")
}
def main(args:Array[String]) {
TimeZone.setDefault(TimeZone.getTimeZone("UTC"))
val parser = new CmdLineParser(Settings)
try {
parser.parseArgument(args:_*)
} catch {
case e:CmdLineException => {
System.err.println(e)
parser.printUsage(System.err)
System.exit(-1)
}
}
if (Settings.showHelp) {
parser.printUsage(System.err)
System.exit(0)
}
if (Settings.logbackConfigFile != null) {
System.setProperty("logback.configurationFile",Settings.logbackConfigFile);
} else if (System.getProperty("logback.configurationFile") == null) {
System.setProperty("logback.configurationFile",
if(Settings.debugMode) "logback.debug.xml" else "logback.xml"
)
}
val jedis = new Jedis(Settings.redisHostname, Settings.redisPort)
loadScript(jedis)
Settings.redisQueues.split(",").
foreach{ queue =>
val t = new Thread(new QueueProcessor(queue))
t.setName(queue)
t.setDaemon(false)
t.start()
}
}
}