solis 0.128.0 → 0.129.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/lib/solis/model.rb +13 -3
- data/lib/solis/query/construct.rb +4 -0
- data/lib/solis/store/sparql/client.rb +102 -22
- data/lib/solis/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: c4f9f15ce1664f194768963f29ad7d3edc8e394a4970e443ab27b7dde1a61e35
|
|
4
|
+
data.tar.gz: d2591a1d9481ce5e7b1577fedd22ae8a93ee096acdc46838fdb07770967e2c6d
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: ef99b8aab43b5c0a534afcf6caef943567664d596ad878c2e08a14465402955d3c88930f80c7294acaa5626d784cef4ab62f42dfbbc6b1c19a55103f47aed80a
|
|
7
|
+
data.tar.gz: 0d21f625fc63af46c3817ba70f3bb334487ac99b0951cdfe40f549938e41096748a2d70a1a78e034e11b0d7c7bf78604bcfd829ceadf19f2447609da59368d20
|
data/lib/solis/model.rb
CHANGED
|
@@ -156,7 +156,8 @@ values ?s {<#{self.graph_id}>}
|
|
|
156
156
|
|
|
157
157
|
def save(validate_dependencies = true, top_level = true)
|
|
158
158
|
raise "I need a SPARQL endpoint" if self.class.sparql_endpoint.nil?
|
|
159
|
-
|
|
159
|
+
pool = Solis::Store::Sparql::Client.pool_for(self.class.sparql_endpoint)
|
|
160
|
+
sparql = pool.checkout
|
|
160
161
|
|
|
161
162
|
before_create_proc&.call(self)
|
|
162
163
|
|
|
@@ -225,6 +226,8 @@ values ?s {<#{self.graph_id}>}
|
|
|
225
226
|
rescue StandardError => e
|
|
226
227
|
Solis::LOGGER.error e.message
|
|
227
228
|
raise e
|
|
229
|
+
ensure
|
|
230
|
+
pool.checkin if pool && sparql
|
|
228
231
|
end
|
|
229
232
|
|
|
230
233
|
# Update an entity.
|
|
@@ -249,7 +252,8 @@ values ?s {<#{self.graph_id}>}
|
|
|
249
252
|
raise "id is mandatory when updating" unless attributes.keys.include?('id')
|
|
250
253
|
|
|
251
254
|
id = attributes.delete('id')
|
|
252
|
-
|
|
255
|
+
pool = Solis::Store::Sparql::Client.pool_for(self.class.sparql_endpoint) if sparql_client.nil?
|
|
256
|
+
sparql = sparql_client || pool.checkout
|
|
253
257
|
|
|
254
258
|
# prefetched_original is used only when it is a complete entity; an id-only stub
|
|
255
259
|
# cannot seed updated_klass (omitted mandatory attributes would be lost).
|
|
@@ -419,6 +423,9 @@ values ?s {<#{self.graph_id}>}
|
|
|
419
423
|
sparql.insert_data(original_graph, graph: original_graph.name) if defined?(original_graph) && original_graph && defined?(sparql) && sparql
|
|
420
424
|
|
|
421
425
|
raise e
|
|
426
|
+
ensure
|
|
427
|
+
# Only return a client this call borrowed itself; a passed-in sparql_client belongs to the caller.
|
|
428
|
+
pool.checkin if pool && sparql
|
|
422
429
|
end
|
|
423
430
|
|
|
424
431
|
def graph_id
|
|
@@ -443,7 +450,8 @@ values ?s {<#{self.graph_id}>}
|
|
|
443
450
|
raise "I need a SPARQL endpoint" if sparql_endpoint.nil?
|
|
444
451
|
return [] if entities.empty?
|
|
445
452
|
|
|
446
|
-
|
|
453
|
+
pool = Solis::Store::Sparql::Client.pool_for(sparql_endpoint)
|
|
454
|
+
sparql = pool.checkout
|
|
447
455
|
|
|
448
456
|
# Batch check existence of all entities
|
|
449
457
|
existing_ids = batch_exists?(sparql, entities)
|
|
@@ -487,6 +495,8 @@ values ?s {<#{self.graph_id}>}
|
|
|
487
495
|
end
|
|
488
496
|
|
|
489
497
|
entities
|
|
498
|
+
ensure
|
|
499
|
+
pool.checkin if pool && sparql
|
|
490
500
|
end
|
|
491
501
|
|
|
492
502
|
# Check existence of multiple entities in a single SPARQL query
|
|
@@ -56,6 +56,10 @@ module Solis
|
|
|
56
56
|
end
|
|
57
57
|
|
|
58
58
|
#SPARQL::Client.new(@sparql_endpoint, graph: @model.class.graph_name, read_timeout: 120)
|
|
59
|
+
# Querying the live endpoint goes through the shared pool; an in-memory
|
|
60
|
+
# repository (constructed or cached result) needs no HTTP connection.
|
|
61
|
+
return @sparql_client unless sparql_repository.is_a?(RDF::Queryable)
|
|
62
|
+
|
|
59
63
|
SPARQL::Client.new(sparql_repository, read_timeout: 120)
|
|
60
64
|
end
|
|
61
65
|
|
|
@@ -6,39 +6,119 @@ require_relative 'client/query'
|
|
|
6
6
|
module Solis
|
|
7
7
|
module Store
|
|
8
8
|
module Sparql
|
|
9
|
+
# Thin wrapper around a process-wide pool of SPARQL::Client instances.
|
|
10
|
+
#
|
|
11
|
+
# Every SPARQL::Client over HTTP owns a Net::HTTP::Persistent keep-alive
|
|
12
|
+
# connection that is only closed by #close or by the garbage collector.
|
|
13
|
+
# Creating clients per request therefore leaks idle connections to the triple
|
|
14
|
+
# store (Virtuoso: "number of licensed connections exceeded"). All instances
|
|
15
|
+
# of this class share one bounded pool per endpoint/graph/read_timeout, so a
|
|
16
|
+
# process never holds more than `pool_size` connections per pool.
|
|
17
|
+
#
|
|
18
|
+
# Pool size: Solis::Options[:sparql_pool_size], else ENV['SOLIS_SPARQL_POOL_SIZE'], else 5.
|
|
19
|
+
# Keep it at or slightly above the number of request threads (e.g. Puma max threads).
|
|
20
|
+
# connection_pool >= 2.5 rebuilds pools in forked children (Puma workers).
|
|
9
21
|
class Client
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
@logger = options[:logger] || Solis::LOGGER
|
|
22
|
+
DEFAULT_POOL_SIZE = 5
|
|
23
|
+
DEFAULT_POOL_TIMEOUT = 160
|
|
24
|
+
DEFAULT_READ_TIMEOUT = 120
|
|
25
|
+
UP_CHECK_INTERVAL = 30
|
|
15
26
|
|
|
16
|
-
|
|
17
|
-
|
|
27
|
+
@pools = {}
|
|
28
|
+
@up_checks = {}
|
|
29
|
+
@mutex = Mutex.new
|
|
18
30
|
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
31
|
+
class << self
|
|
32
|
+
# Shared pool for an endpoint. graph_name nil means: no graph option on the client.
|
|
33
|
+
def pool_for(endpoint, graph_name: nil, read_timeout: DEFAULT_READ_TIMEOUT, logger: nil)
|
|
34
|
+
key = pool_key(endpoint, graph_name, read_timeout)
|
|
35
|
+
@mutex.synchronize do
|
|
36
|
+
@pools[key] ||= ConnectionPool.new(size: pool_size, timeout: DEFAULT_POOL_TIMEOUT) do
|
|
37
|
+
Solis::LOGGER.level = Logger::DEBUG if ConfigFile[:debug]
|
|
38
|
+
options = { read_timeout: read_timeout, logger: logger || Solis::LOGGER }
|
|
39
|
+
options[:graph] = graph_name unless graph_name.nil?
|
|
40
|
+
SPARQL::Client.new(endpoint, **options)
|
|
23
41
|
end
|
|
42
|
+
end
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
# Borrow a raw SPARQL::Client from the shared pool. Re-entrant per thread.
|
|
46
|
+
def with(endpoint, graph_name: nil, read_timeout: DEFAULT_READ_TIMEOUT, &block)
|
|
47
|
+
pool_for(endpoint, graph_name: graph_name, read_timeout: read_timeout).with(&block)
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
# Close every pooled connection (e.g. on shutdown or in tests).
|
|
51
|
+
def shutdown_all
|
|
52
|
+
pools = @mutex.synchronize do
|
|
53
|
+
current = @pools.values
|
|
54
|
+
@pools = {}
|
|
55
|
+
@up_checks = {}
|
|
56
|
+
current
|
|
57
|
+
end
|
|
58
|
+
pools.each { |pool| pool.shutdown { |client| client.close rescue nil } }
|
|
59
|
+
end
|
|
60
|
+
|
|
61
|
+
def pool_count
|
|
62
|
+
@mutex.synchronize { @pools.size }
|
|
63
|
+
end
|
|
64
|
+
|
|
65
|
+
def pool_size
|
|
66
|
+
configured = (Solis::Options.instance.get[:sparql_pool_size] rescue nil) || ENV['SOLIS_SPARQL_POOL_SIZE']
|
|
67
|
+
size = configured.to_i
|
|
68
|
+
size > 0 ? size : DEFAULT_POOL_SIZE
|
|
69
|
+
end
|
|
70
|
+
|
|
71
|
+
def cached_up(key)
|
|
72
|
+
@mutex.synchronize do
|
|
73
|
+
check = @up_checks[key]
|
|
74
|
+
check && (monotonic_now - check[:at]) < UP_CHECK_INTERVAL ? check : nil
|
|
75
|
+
end
|
|
76
|
+
end
|
|
77
|
+
|
|
78
|
+
def store_up(key, result)
|
|
79
|
+
@mutex.synchronize { @up_checks[key] = { at: monotonic_now, result: result } }
|
|
80
|
+
end
|
|
24
81
|
|
|
25
|
-
|
|
82
|
+
def clear_up(key)
|
|
83
|
+
@mutex.synchronize { @up_checks.delete(key) }
|
|
26
84
|
end
|
|
85
|
+
|
|
86
|
+
def pool_key(endpoint, graph_name, read_timeout)
|
|
87
|
+
[endpoint.to_s, graph_name.nil? ? nil : graph_name.to_s, read_timeout.to_i]
|
|
88
|
+
end
|
|
89
|
+
|
|
90
|
+
def monotonic_now
|
|
91
|
+
Process.clock_gettime(Process::CLOCK_MONOTONIC)
|
|
92
|
+
end
|
|
93
|
+
end
|
|
94
|
+
|
|
95
|
+
def initialize(endpoint, options = {})
|
|
96
|
+
@endpoint = endpoint
|
|
97
|
+
@graph_name = options[:graph_name] || ''
|
|
98
|
+
@read_timeout = options[:read_timeout] || DEFAULT_READ_TIMEOUT
|
|
99
|
+
@logger = options[:logger] || Solis::LOGGER
|
|
100
|
+
@key = self.class.pool_key(@endpoint, @graph_name, @read_timeout)
|
|
101
|
+
@pool = self.class.pool_for(@endpoint, graph_name: @graph_name, read_timeout: @read_timeout, logger: @logger)
|
|
102
|
+
end
|
|
103
|
+
|
|
104
|
+
def with(&block)
|
|
105
|
+
@pool.with(&block)
|
|
27
106
|
end
|
|
28
107
|
|
|
108
|
+
# The result is cached per pool for UP_CHECK_INTERVAL seconds across all instances,
|
|
109
|
+
# so a request no longer pays an extra ASK round trip.
|
|
29
110
|
def up?
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
@up_result = nil
|
|
111
|
+
cached = self.class.cached_up(@key)
|
|
112
|
+
return cached[:result] if cached
|
|
113
|
+
|
|
114
|
+
result = nil
|
|
35
115
|
@pool.with do |c|
|
|
36
|
-
|
|
116
|
+
result = c.query("ASK WHERE { ?s ?p ?o }")
|
|
37
117
|
end
|
|
38
|
-
@
|
|
39
|
-
|
|
118
|
+
self.class.store_up(@key, result)
|
|
119
|
+
result
|
|
40
120
|
rescue HTTP::Error => e
|
|
41
|
-
@
|
|
121
|
+
self.class.clear_up(@key)
|
|
42
122
|
return false
|
|
43
123
|
end
|
|
44
124
|
|
|
@@ -53,4 +133,4 @@ module Solis
|
|
|
53
133
|
end
|
|
54
134
|
end
|
|
55
135
|
end
|
|
56
|
-
end
|
|
136
|
+
end
|
data/lib/solis/version.rb
CHANGED