Skip to content

Commit a7971e1

Browse files
committed
Merge branch 'master' into 1.0
2 parents e6a6537 + be34dea commit a7971e1

File tree

9 files changed

+127
-96
lines changed

9 files changed

+127
-96
lines changed

README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ Creating a JDBC river is easy:
2121

2222
- install the plugin
2323

24-
- download a JDBC driver jar from your vendor's site (for example MySQL) and put the jar into the folder of the plugin `$ES_HOME/plugins/river-jdbc`.
24+
- download a JDBC driver jar from your vendor's site (for example MySQL) and put the jar into the folder of the plugin `$ES_HOME/plugins/jdbc`.
2525

2626
Assuming you have a table of name `orders`, you can issue this simple command from the command line
2727

@@ -159,7 +159,7 @@ Internet access (of course)
159159
5. Add MySQL JDBC driver jar to JDBC river plugin directory and set access permission for .jar file (at least chmod 644)
160160

161161
`cp mysql-connector-java-5.1.33-bin.jar $ES_HOME/plugins/jdbc/`
162-
`chmod 644 $ES_HOME/plugins/jdbc/`
162+
`chmod 644 $ES_HOME/plugins/jdbc/*`
163163

164164
6. Start elasticsearch from terminal window
165165

src/main/java/org/xbib/elasticsearch/plugin/jdbc/cron/CronThreadPoolExecutor.java

+9-9
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@
1515
*/
1616
package org.xbib.elasticsearch.plugin.jdbc.cron;
1717

18+
import org.elasticsearch.common.logging.ESLogger;
19+
import org.elasticsearch.common.logging.ESLoggerFactory;
20+
1821
import java.util.Date;
1922
import java.util.concurrent.CancellationException;
2023
import java.util.concurrent.Future;
@@ -29,6 +32,9 @@
2932
* to calculate future execution times for scheduled tasks.
3033
*/
3134
public class CronThreadPoolExecutor extends ScheduledThreadPoolExecutor implements CronExecutorService {
35+
36+
private final static ESLogger logger = ESLoggerFactory.getLogger("river.jdbc.CronThreadPoolExecutor");
37+
3238
/**
3339
* Constructs a new CronThreadPoolExecutor.
3440
*
@@ -74,11 +80,8 @@ public Future<?> schedule(final Runnable task, final CronExpression expression)
7480
if (task == null) {
7581
throw new NullPointerException();
7682
}
77-
this.setCorePoolSize(this.getCorePoolSize() + 1);
83+
setCorePoolSize(getCorePoolSize() + 1);
7884
Runnable scheduleTask = new Runnable() {
79-
/**
80-
* @see Runnable#run()
81-
*/
8285
@Override
8386
public void run() {
8487
Date now = new Date();
@@ -92,13 +95,10 @@ public void run() {
9295
}
9396
time = expression.getNextValidTimeAfter(now);
9497
}
95-
} catch (RejectedExecutionException e) {
96-
//
97-
} catch (CancellationException e) {
98-
//
9998
} catch (InterruptedException e) {
100-
//
10199
Thread.currentThread().interrupt();
100+
} catch (RejectedExecutionException | CancellationException e) {
101+
logger.error(e.getMessage(), e);
102102
}
103103
}
104104
};

src/main/java/org/xbib/elasticsearch/plugin/jdbc/feeder/JDBCFeeder.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ private List<Future<?>> schedule(Thread thread) {
186186
Long seconds = settings.getAsTime("interval", TimeValue.timeValueSeconds(0)).seconds();
187187
if (schedule != null && schedule.length > 0) {
188188
CronThreadPoolExecutor cronThreadPoolExecutor =
189-
new CronThreadPoolExecutor(settings.getAsInt("threadpoolsize", 4));
189+
new CronThreadPoolExecutor(settings.getAsInt("threadpoolsize", 1));
190190
for (String cron : schedule) {
191191
futures.add(cronThreadPoolExecutor.schedule(thread, new CronExpression(cron)));
192192
}

src/main/java/org/xbib/elasticsearch/plugin/jdbc/river/JDBCRiver.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ private List<Future<?>> schedule(Thread thread) {
189189
List<Future<?>> futures = newLinkedList();
190190
if (schedule != null && schedule.length > 0) {
191191
CronThreadPoolExecutor cronThreadPoolExecutor =
192-
new CronThreadPoolExecutor(settings.getAsInt("threadpoolsize", 4));
192+
new CronThreadPoolExecutor(settings.getAsInt("threadpoolsize", 1));
193193
for (String cron : schedule) {
194194
futures.add(cronThreadPoolExecutor.schedule(thread, new CronExpression(cron)));
195195
}

src/main/java/org/xbib/elasticsearch/plugin/jdbc/state/RiverState.java

+75-43
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,6 @@
4141
*/
4242
public class RiverState implements Streamable, ToXContent, Comparable<RiverState> {
4343

44-
private final static DateTime EMPTY_DATETIME = new DateTime(0L);
45-
4644
/**
4745
* The name of the river instance
4846
*/
@@ -61,12 +59,12 @@ public class RiverState implements Streamable, ToXContent, Comparable<RiverState
6159
/*
6260
* The time of the last river activity
6361
*/
64-
private DateTime begin;
62+
private DateTime lastActiveBegin;
6563

6664
/*
6765
* The time when the last river activity ended
6866
*/
69-
private DateTime end;
67+
private DateTime lastActiveEnd;
7068

7169
/**
7270
* A custom map for more information about the river
@@ -120,43 +118,23 @@ public DateTime getStarted() {
120118
* @return this state
121119
*/
122120
public RiverState setLastActive(DateTime begin, DateTime end) {
123-
if (begin != null) {
124-
this.begin = begin;
125-
}
126-
if (end != null) {
127-
this.end = end;
128-
}
121+
this.lastActiveBegin = begin;
122+
this.lastActiveEnd = end;
129123
return this;
130124
}
131125

132126
/**
133127
* @return the begin of the last river activity
134128
*/
135129
public DateTime getLastActiveBegin() {
136-
return begin != null ? begin : EMPTY_DATETIME;
130+
return lastActiveBegin;
137131
}
138132

139133
/**
140134
* @return the end of the last river activity
141135
*/
142136
public DateTime getLastActiveEnd() {
143-
return end != null ? end : EMPTY_DATETIME;
144-
}
145-
146-
/**
147-
* Was the river active at a certain time? Only the last activity can be checked.
148-
*
149-
* @param instant the time to check
150-
* @return true if river was active, false if not
151-
*/
152-
public boolean wasActiveAt(DateTime instant) {
153-
return instant != null
154-
&& begin != null && begin.getMillis() != 0L && begin.isBefore(instant)
155-
&& (end == null || end.getMillis() == 0L || end.isAfter(instant));
156-
}
157-
158-
public boolean wasInactiveAt(DateTime instant) {
159-
return !wasActiveAt(instant);
137+
return lastActiveEnd;
160138
}
161139

162140
public RiverState setCounter(Integer counter) {
@@ -178,19 +156,51 @@ public Map<String, Object> getCustom() {
178156
return (Map<String, Object>) this.map.get("custom");
179157
}
180158

181-
public boolean isAborted() {
182-
return map.containsKey("aborted") ? (Boolean) map.get("aborted") : false;
183-
}
184-
185159
public boolean isSuspended() {
186160
return map.containsKey("suspended") ? (Boolean) map.get("suspended") : false;
187161
}
188162

163+
public RiverState setLastStartDate(long lastStartDate) {
164+
this.map.put("lastStartDate", lastStartDate);
165+
return this;
166+
}
167+
168+
public long getLastStartDate() {
169+
return (long)this.map.get("lastStartDate");
170+
}
171+
172+
public RiverState setLastEndDate(long lastEndDate) {
173+
this.map.put("lastEndDate", lastEndDate);
174+
return this;
175+
}
176+
177+
public long getLastEndDate() {
178+
return (long)this.map.get("lastEndDate");
179+
}
180+
181+
public RiverState setLastExecutionStartDate(long lastExecutionStartDate) {
182+
this.map.put("lastExecutionStartDate", lastExecutionStartDate);
183+
return this;
184+
}
185+
186+
public long getLastExecutionStartDate() {
187+
return (long)this.map.get("lastExecutionStartDate");
188+
}
189+
190+
public RiverState setLastExecutionEndDate(long lastExecutionEndDate) {
191+
this.map.put("lastExecutionEndDate", lastExecutionEndDate);
192+
return this;
193+
}
194+
195+
public long getLastExecutionEndDate() {
196+
return (long)this.map.get("lastExecutionEndDate");
197+
}
198+
189199
public RiverState fromXContent(XContentParser parser) throws IOException {
190200
DateTimeFormatter dateTimeFormatter = ISODateTimeFormat.dateOptionalTimeParser().withZone(DateTimeZone.UTC);
191201
Long startTimestamp = 0L;
192-
Long begin = 0L;
193-
Long end = 0L;
202+
Long begin = null;
203+
Long end = null;
194204
String name = null;
195205
String type = null;
196206
String currentFieldName = null;
@@ -213,11 +223,11 @@ public RiverState fromXContent(XContentParser parser) throws IOException {
213223
break;
214224
case "last_active_begin":
215225
begin = parser.text() != null && !"null".equals(parser.text()) ?
216-
dateTimeFormatter.parseMillis(parser.text()) : 0L;
226+
dateTimeFormatter.parseMillis(parser.text()) : null;
217227
break;
218228
case "last_active_end":
219229
end = parser.text() != null && !"null".equals(parser.text()) ?
220-
dateTimeFormatter.parseMillis(parser.text()) : 0L;
230+
dateTimeFormatter.parseMillis(parser.text()) : null;
221231
break;
222232
}
223233
} else if (token == START_OBJECT) {
@@ -228,7 +238,8 @@ public RiverState fromXContent(XContentParser parser) throws IOException {
228238
.setName(name)
229239
.setType(type)
230240
.setStarted(new DateTime(startTimestamp))
231-
.setLastActive(new DateTime(begin), new DateTime(end))
241+
.setLastActive(begin != null ? new DateTime(begin) : null,
242+
end != null ? new DateTime(end) : null)
232243
.setMap(map);
233244
}
234245

@@ -250,19 +261,40 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws
250261
public void readFrom(StreamInput in) throws IOException {
251262
this.name = in.readOptionalString();
252263
this.type = in.readOptionalString();
253-
this.started = new DateTime(in.readLong());
254-
this.begin = new DateTime(in.readLong());
255-
this.end = new DateTime(in.readLong());
264+
if (in.readBoolean()) {
265+
this.started = new DateTime(in.readLong());
266+
}
267+
if (in.readBoolean()) {
268+
this.lastActiveBegin = new DateTime(in.readLong());
269+
}
270+
if (in.readBoolean()) {
271+
this.lastActiveEnd = new DateTime(in.readLong());
272+
}
256273
map = in.readMap();
257274
}
258275

259276
@Override
260277
public void writeTo(StreamOutput out) throws IOException {
261278
out.writeOptionalString(name);
262279
out.writeOptionalString(type);
263-
out.writeLong(started != null ? started.getMillis() : 0L);
264-
out.writeLong(begin != null ? begin.getMillis() : 0L);
265-
out.writeLong(end != null ? end.getMillis() : 0L);
280+
if (started != null) {
281+
out.writeBoolean(true);
282+
out.writeLong(started.getMillis());
283+
} else {
284+
out.writeBoolean(false);
285+
}
286+
if (lastActiveBegin != null) {
287+
out.writeBoolean(true);
288+
out.writeLong(lastActiveBegin.getMillis());
289+
} else {
290+
out.writeBoolean(false);
291+
}
292+
if (lastActiveEnd != null) {
293+
out.writeBoolean(true);
294+
out.writeLong(lastActiveEnd.getMillis());
295+
} else {
296+
out.writeBoolean(false);
297+
}
266298
out.writeMap(map);
267299
}
268300

src/main/java/org/xbib/elasticsearch/river/jdbc/strategy/simple/SimpleRiverContext.java

+12-20
Original file line numberDiff line numberDiff line change
@@ -110,14 +110,6 @@ public class SimpleRiverContext implements RiverContext {
110110

111111
private long lastRowCount;
112112

113-
private long lastStartDate;
114-
115-
private long lastEndDate;
116-
117-
private long lastExecutionStartDate;
118-
119-
private long lastExecutionEndDate;
120-
121113
private Map<String, Object> columnNameMap;
122114

123115
private Map<String, Object> lastRow = new HashMap<String, Object>();
@@ -337,39 +329,39 @@ public long getLastRowCount() {
337329
}
338330

339331
public SimpleRiverContext setLastStartDate(long lastStartDate) {
340-
this.lastStartDate = lastStartDate;
332+
riverState.setLastStartDate(lastStartDate);
341333
return this;
342334
}
343335

344336
public long getLastStartDate() {
345-
return lastStartDate;
337+
return riverState.getLastStartDate();
346338
}
347339

348340
public SimpleRiverContext setLastEndDate(long lastEndDate) {
349-
this.lastEndDate = lastEndDate;
341+
riverState.setLastEndDate(lastEndDate);
350342
return this;
351343
}
352344

353345
public long getLastEndDate() {
354-
return lastEndDate;
346+
return riverState.getLastEndDate();
355347
}
356348

357349
public SimpleRiverContext setLastExecutionStartDate(long lastExecutionStartDate) {
358-
this.lastExecutionStartDate = lastExecutionStartDate;
350+
riverState.setLastExecutionStartDate(lastExecutionStartDate);
359351
return this;
360352
}
361353

362354
public long getLastExecutionStartDate() {
363-
return lastExecutionStartDate;
355+
return riverState.getLastExecutionStartDate();
364356
}
365357

366358
public SimpleRiverContext setLastExecutionEndDate(long lastExecutionEndDate) {
367-
this.lastExecutionEndDate = lastExecutionEndDate;
359+
riverState.setLastExecutionEndDate(lastExecutionEndDate);
368360
return this;
369361
}
370362

371363
public long getLastExecutionEndDate() {
372-
return lastExecutionEndDate;
364+
return riverState.getLastExecutionEndDate();
373365
}
374366

375367
public SimpleRiverContext setColumnNameMap(Map<String, Object> columnNameMap) {
@@ -473,10 +465,10 @@ public Map<String, Object> asMap() {
473465
.field("shouldignorenull", shouldIgnoreNull)
474466
.field("lastResultSetMetadata", lastResultSetMetadata)
475467
.field("lastDatabaseMetadata", lastDatabaseMetadata)
476-
.field("lastStartDate", lastStartDate)
477-
.field("lastEndDate", lastEndDate)
478-
.field("lastExecutionStartDate", lastExecutionStartDate)
479-
.field("lastExecutionEndDate", lastExecutionEndDate)
468+
.field("lastStartDate", riverState.getLastStartDate())
469+
.field("lastEndDate", riverState.getLastEndDate())
470+
.field("lastExecutionStartDate", riverState.getLastExecutionStartDate())
471+
.field("lastExecutionEndDate", riverState.getLastExecutionEndDate())
480472
.field("columnNameMap", columnNameMap)
481473
.field("lastRow", lastRow)
482474
.field("sql", sql)

0 commit comments

Comments
 (0)