-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Expand file tree
/
Copy pathBigQueryDaemonPollingTask.java
More file actions
122 lines (107 loc) · 4.62 KB
/
Copy pathBigQueryDaemonPollingTask.java
File metadata and controls
122 lines (107 loc) · 4.62 KB
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
/*
* Copyright 2023 Google LLC
*
* Licensed 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
*
* https://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.
*/
package com.google.cloud.bigquery.jdbc;
import com.google.api.core.InternalApi;
import com.google.cloud.bigquery.exception.BigQueryJdbcRuntimeException;
import java.lang.ref.Reference;
import java.lang.ref.ReferenceQueue;
import java.util.List;
/**
* This class is used to add polling threads required for polling the reference queues associated
* with the two ResultSets
*/
@InternalApi
class BigQueryDaemonPollingTask extends Thread {
private static final BigQueryJdbcCustomLogger LOG =
new BigQueryJdbcCustomLogger(BigQueryDaemonPollingTask.class.getName());
static ReferenceQueue<BigQueryArrowResultSet> referenceQueueArrowRs;
static ReferenceQueue<BigQueryJsonResultSet> referenceQueueJsonRs;
static List<BigQueryResultSetFinalizers.ArrowResultSetFinalizer> arrowRsFinalizers;
static List<BigQueryResultSetFinalizers.JsonResultSetFinalizer> jsonRsFinalizers;
private static BigQueryDaemonPollingTask arrowDaemon;
private static BigQueryDaemonPollingTask jsonDaemon;
private static final Object LOCK = new Object();
private BigQueryDaemonPollingTask(
List<BigQueryResultSetFinalizers.ArrowResultSetFinalizer> arrowRsFinalizers,
ReferenceQueue<BigQueryArrowResultSet> referenceQueueArrowRs) {
BigQueryDaemonPollingTask.referenceQueueArrowRs = referenceQueueArrowRs;
BigQueryDaemonPollingTask.arrowRsFinalizers = arrowRsFinalizers;
setDaemon(true);
}
private BigQueryDaemonPollingTask(
ReferenceQueue<BigQueryJsonResultSet> referenceQueueJsonRs,
List<BigQueryResultSetFinalizers.JsonResultSetFinalizer> jsonRsFinalizers) {
BigQueryDaemonPollingTask.referenceQueueJsonRs = referenceQueueJsonRs;
BigQueryDaemonPollingTask.jsonRsFinalizers = jsonRsFinalizers;
setDaemon(true);
}
/**
* ThreadSafe method which creates two instances of polling task, one each for each type of
* ResultSet
*
* @param referenceQueueArrowRs ReferenceQueue for ArrowResultSet
* @param referenceQueueJsonRs ReferenceQueue for JsonResultSet
* @param arrowRsFinalizers Finalizer for ArrowResultSet
* @param jsonRsFinalizers Finalizer for JsonResultSet
* @return true if the tasks were created
*/
public static boolean startGcDaemonTask(
ReferenceQueue<BigQueryArrowResultSet> referenceQueueArrowRs,
ReferenceQueue<BigQueryJsonResultSet> referenceQueueJsonRs,
List<BigQueryResultSetFinalizers.ArrowResultSetFinalizer> arrowRsFinalizers,
List<BigQueryResultSetFinalizers.JsonResultSetFinalizer> jsonRsFinalizers) {
LOG.finest("++enter++");
synchronized (LOCK) {
// 2 Background threads will be required to monitor the respective queues
if (arrowDaemon == null && jsonDaemon == null) {
arrowDaemon = new BigQueryDaemonPollingTask(arrowRsFinalizers, referenceQueueArrowRs);
arrowDaemon.start();
jsonDaemon = new BigQueryDaemonPollingTask(referenceQueueJsonRs, jsonRsFinalizers);
jsonDaemon.start();
return true;
}
}
// Task(s) are already initialised
return false;
}
@Override
public void run() {
Reference<?> reference;
try {
LOG.finest("++enter++");
// poll for Arrow ResultSets
if (referenceQueueArrowRs != null) {
while ((reference = referenceQueueArrowRs.remove()) != null) {
LOG.fine("Clearing Arrow ResultSet reference " + referenceQueueArrowRs);
((BigQueryResultSetFinalizers.ArrowResultSetFinalizer) reference).finalizeResources();
reference.clear();
}
}
// poll for JSON ResultSets
else if (referenceQueueJsonRs != null) {
while ((reference = referenceQueueJsonRs.remove()) != null) {
LOG.fine("Clearing Json ResultSet reference " + referenceQueueJsonRs);
((BigQueryResultSetFinalizers.JsonResultSetFinalizer) reference).finalizeResources();
reference.clear();
}
} else {
throw new BigQueryJdbcRuntimeException("Null Reference Queue");
}
} catch (InterruptedException ex) {
throw new BigQueryJdbcRuntimeException("Interrupted in GC daemon task", ex);
}
}
}