Skip to content

Commit 2b9cd80

Browse files
committed
Change of threading policy, configurable timeout
1 parent bd47f45 commit 2b9cd80

File tree

5 files changed

+82
-14
lines changed

5 files changed

+82
-14
lines changed

app/save-and-restore/util/src/main/java/org/phoebus/saveandrestore/util/SnapshotUtil.java

Lines changed: 52 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,6 @@ public class SnapshotUtil {
4141

4242
private final Logger LOG = Logger.getLogger(SnapshotUtil.class.getName());
4343

44-
private final int connectionTimeout = Preferences.connectionTimeout;
45-
4644
private final ExecutorService executorService = Executors.newFixedThreadPool(10);
4745

4846
public SnapshotUtil() {
@@ -70,13 +68,30 @@ public SnapshotUtil() {
7068
* @param snapshotItems {@link SnapshotItem}
7169
*/
7270
public synchronized List<RestoreResult> restore(List<SnapshotItem> snapshotItems) {
71+
return restore(snapshotItems, Preferences.connectionTimeout);
72+
}
73+
74+
/**
75+
* Restore PV values from a list of snapshot items
76+
*
77+
* <p>
78+
* Writes concurrently the pv value to the non-null set PVs in
79+
* the snapshot items.
80+
* Uses synchronized to ensure only one frontend can write at a time.
81+
* Returns a list of the snapshot items you have set, with an error message if
82+
* an error occurred.
83+
*
84+
* @param snapshotItems {@link SnapshotItem}
85+
* @param connectionTimeout The timeout in ms to use for EPICS connection.
86+
*/
87+
public synchronized List<RestoreResult> restore(List<SnapshotItem> snapshotItems, long connectionTimeout) {
7388
// First clean the list of SnapshotItems from read-only elements.
7489
List<SnapshotItem> cleanedSnapshotItems = cleanSnapshotItems(snapshotItems);
7590
List<RestoreResult> restoreResultList = new ArrayList<>();
7691

7792
List<RestoreCallable> callables = new ArrayList<>();
7893
for (SnapshotItem si : cleanedSnapshotItems) {
79-
RestoreCallable restoreCallable = new RestoreCallable(si, restoreResultList);
94+
RestoreCallable restoreCallable = new RestoreCallable(si, restoreResultList, connectionTimeout);
8095
callables.add(restoreCallable);
8196
}
8297

@@ -98,25 +113,53 @@ public synchronized List<RestoreResult> restore(List<SnapshotItem> snapshotItems
98113
* {@link ConfigPv} item in {@link ConfigurationData} a {@link SnapshotItem} is created holding the
99114
* values read.
100115
* Read operations are concurrent using a thread pool. Failed connections/reads will cause a wait of at most
101-
* {@link #connectionTimeout} ms on each thread.
116+
* {@link Preferences#connectionTimeout} ms on each thread.
102117
*
103118
* @param configurationData Identifies which {@link Configuration} user selected to create a snapshot.
104119
* @return A list of {@link SnapshotItem}s holding the values read from IOCs.
105120
*/
106121
public List<SnapshotItem> takeSnapshot(ConfigurationData configurationData) {
107-
return takeSnapshot(configurationData.getPvList());
122+
return takeSnapshot(configurationData.getPvList(), Preferences.connectionTimeout);
123+
}
124+
125+
/**
126+
* Reads all PVs and read-back PVs as defined in the {@link ConfigurationData} argument. For each
127+
* {@link ConfigPv} item in {@link ConfigurationData} a {@link SnapshotItem} is created holding the
128+
* values read.
129+
* Read operations are concurrent using a thread pool. Failed connections/reads will cause a wait of at most
130+
* <code>connectionTimeout</code> ms on each thread.
131+
*
132+
* @param configurationData Identifies which {@link Configuration} user selected to create a snapshot.
133+
* @return A list of {@link SnapshotItem}s holding the values read from IOCs.
134+
*/
135+
public List<SnapshotItem> takeSnapshot(ConfigurationData configurationData, long connectionTimeout) {
136+
return takeSnapshot(configurationData.getPvList(), connectionTimeout);
108137
}
109138

110139
/**
111140
* Reads all PVs and read-back PVs as defined in the {@link ConfigurationData} argument. For each
112141
* {@link ConfigPv} item in {@link ConfigurationData} a {@link SnapshotItem} is created.
113142
* Read operations are concurrent using a thread pool. Failed connections/reads will cause a wait of at most
114-
* {@link #connectionTimeout} ms on each thread.
143+
* {@link Preferences#connectionTimeout} ms on each thread.
115144
*
116145
* @param configPvs List of {@link ConfigPv}s defining a {@link Configuration}.
117146
* @return A list of {@link SnapshotItem}s holding the values read from IOCs.
118147
*/
119148
public List<SnapshotItem> takeSnapshot(final List<ConfigPv> configPvs) {
149+
return takeSnapshot(configPvs, Preferences.connectionTimeout);
150+
}
151+
152+
/**
153+
* Reads all PVs and read-back PVs as defined in the {@link ConfigurationData} argument. For each
154+
* {@link ConfigPv} item in {@link ConfigurationData} a {@link SnapshotItem} is created.
155+
* Read operations are concurrent using a thread pool. Failed connections/reads will cause a wait of at most
156+
* <code>connectionTimeout</code> ms on each thread.
157+
*
158+
* @param configPvs List of {@link ConfigPv}s defining a {@link Configuration}.
159+
* @param connectionTimeout The timeout in ms to use for EPICS connection.
160+
* @return A list of {@link SnapshotItem}s holding the values read from IOCs.
161+
*/
162+
public List<SnapshotItem> takeSnapshot(final List<ConfigPv> configPvs, long connectionTimeout) {
120163
List<SnapshotItem> snapshotItems = new ArrayList<>();
121164
List<Callable<Void>> callables = new ArrayList<>();
122165
Map<String, VType> pvValues = Collections.synchronizedMap(new HashMap<>());
@@ -232,10 +275,12 @@ private class RestoreCallable implements Callable<Void> {
232275
private final List<RestoreResult> restoreResultList;
233276
private PV pv;
234277
private final SnapshotItem snapshotItem;
278+
private final long connectionTimeout;
235279

236-
public RestoreCallable(SnapshotItem snapshotItem, List<RestoreResult> restoreResultList) {
280+
public RestoreCallable(SnapshotItem snapshotItem, List<RestoreResult> restoreResultList, long connectionTimeout) {
237281
this.snapshotItem = snapshotItem;
238282
this.restoreResultList = restoreResultList;
283+
this.connectionTimeout = connectionTimeout;
239284
}
240285

241286
@Override

services/save-and-restore/src/main/java/org/phoebus/service/saveandrestore/web/config/WebConfiguration.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,10 @@
2121
import org.phoebus.service.saveandrestore.persistence.dao.NodeDAO;
2222
import org.phoebus.service.saveandrestore.persistence.dao.impl.elasticsearch.ElasticsearchDAO;
2323
import org.phoebus.service.saveandrestore.websocket.WebSocket;
24+
import org.springframework.beans.factory.annotation.Value;
2425
import org.springframework.context.annotation.Bean;
2526
import org.springframework.context.annotation.Configuration;
27+
import org.springframework.context.annotation.PropertySource;
2628
import org.springframework.context.annotation.Scope;
2729

2830
import java.util.List;
@@ -34,8 +36,18 @@
3436
* {@link Configuration} class setting up beans for {@link org.springframework.stereotype.Controller} classes.
3537
*/
3638
@Configuration
39+
@PropertySource("classpath:application.properties")
3740
public class WebConfiguration {
3841

42+
@Value("${connection.timeout:5000}")
43+
public long connectionTimeout;
44+
45+
@SuppressWarnings("unused")
46+
@Bean
47+
public long getConnectionTimeout(){
48+
return connectionTimeout;
49+
}
50+
3951
/**
4052
*
4153
* @return A {@link NodeDAO} instance.
@@ -63,11 +75,13 @@ public SnapshotUtil snapshotRestorer(){
6375
return new SnapshotUtil();
6476
}
6577

78+
@SuppressWarnings("unused")
6679
@Bean
6780
public ExecutorService executorService(){
6881
return Executors.newCachedThreadPool();
6982
}
7083

84+
@SuppressWarnings("unused")
7185
@Bean(name = "sockets")
7286
@Scope("singleton")
7387
public List<WebSocket> getSockets() {

services/save-and-restore/src/main/java/org/phoebus/service/saveandrestore/web/controllers/BaseController.java

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
/**
3434
* Base controller that should be extended to make sure exceptions are handled
3535
* properly, i.e. make the service return suitable HTTP status codes.
36+
*
3637
* @author georgweiss
3738
* Created 23 Nov 2018
3839
*/
@@ -44,6 +45,9 @@ public abstract class BaseController {
4445

4546
private final Logger logger = Logger.getLogger(BaseController.class.getName());
4647

48+
@Autowired
49+
public long connectionTimeout;
50+
4751
/**
4852
* Identity of the admin role
4953
*/
@@ -59,7 +63,8 @@ public abstract class BaseController {
5963

6064
/**
6165
* Intercepts {@link SnapshotNotFoundException} and triggers a {@link HttpStatus#NOT_FOUND}.
62-
* @param req The servlet request
66+
*
67+
* @param req The servlet request
6368
* @param exception The exception to intercept
6469
* @return A {@link ResponseEntity} carrying the underlying exception message.
6570
*/
@@ -72,7 +77,8 @@ public ResponseEntity<String> handleSnapshotNotFoundException(HttpServletRequest
7277

7378
/**
7479
* Intercepts {@link IllegalArgumentException} and triggers a {@link HttpStatus#BAD_REQUEST}.
75-
* @param req The servlet request
80+
*
81+
* @param req The servlet request
7682
* @param exception The exception to intercept
7783
* @return A {@link ResponseEntity} carrying the underlying exception message.
7884
*/
@@ -85,7 +91,8 @@ public ResponseEntity<String> handleIllegalArgumentException(HttpServletRequest
8591

8692
/**
8793
* Intercepts {@link NodeNotFoundException} and triggers a {@link HttpStatus#NOT_FOUND}.
88-
* @param req The {@link HttpServlet} request
94+
*
95+
* @param req The {@link HttpServlet} request
8996
* @param exception The exception to intercept
9097
* @return A {@link ResponseEntity} carrying the underlying exception message.
9198
*/

services/save-and-restore/src/main/java/org/phoebus/service/saveandrestore/web/controllers/SnapshotRestoreController.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323
import org.phoebus.saveandrestore.util.SnapshotUtil;
2424
import org.phoebus.service.saveandrestore.persistence.dao.NodeDAO;
2525
import org.springframework.beans.factory.annotation.Autowired;
26+
import org.springframework.beans.factory.annotation.Value;
27+
import org.springframework.context.annotation.PropertySource;
2628
import org.springframework.web.bind.annotation.PostMapping;
2729
import org.springframework.web.bind.annotation.RequestBody;
2830
import org.springframework.web.bind.annotation.RequestParam;
@@ -50,7 +52,7 @@ public class SnapshotRestoreController extends BaseController {
5052
@PostMapping(value = "/restore/items", produces = JSON)
5153
public List<RestoreResult> restoreFromSnapshotItems(
5254
@RequestBody List<SnapshotItem> snapshotItems) {
53-
return snapshotUtil.restore(snapshotItems);
55+
return snapshotUtil.restore(snapshotItems, connectionTimeout);
5456
}
5557

5658
@PostMapping(value = "/restore/node", produces = JSON)
@@ -59,7 +61,7 @@ public List<RestoreResult> restoreFromSnapshotNode(
5961
Node snapshotNode = nodeDAO.getNode(nodeId);
6062
LOG.log(Level.INFO, "Restore requested for snapshot '" + snapshotNode.getName() + "'");
6163
var snapshot = nodeDAO.getSnapshotData(nodeId);
62-
return snapshotUtil.restore(snapshot.getSnapshotItems());
64+
return snapshotUtil.restore(snapshot.getSnapshotItems(), connectionTimeout);
6365
}
6466
}
6567

services/save-and-restore/src/main/java/org/phoebus/service/saveandrestore/web/controllers/TakeSnapshotController.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ public List<SnapshotItem> takeSnapshot(@PathVariable String configNodeId) {
5454
ConfigurationData configurationData = nodeDAO.getConfigurationData(configNodeId);
5555
List<SnapshotItem> snapshotItems;
5656
try {
57-
snapshotItems = snapshotUtil.takeSnapshot(configurationData);
57+
snapshotItems = snapshotUtil.takeSnapshot(configurationData, connectionTimeout);
5858
} catch (Exception e) {
5959
throw new RuntimeException(e);
6060
}
@@ -78,7 +78,7 @@ public List<SnapshotItem> takeSnapshot(@PathVariable String configNodeId) {
7878
public Snapshot takeSnapshotAndSave(@PathVariable String configNodeId,
7979
@RequestParam(name = "name", required = false) String snapshotName,
8080
@RequestParam(name = "comment", required = false) String comment) {
81-
if (snapshotName != null) {
81+
if (snapshotName != null) {
8282
String _snapshotName = snapshotName;
8383
List<Node> childNodes = nodeDAO.getChildNodes(configNodeId);
8484
if (childNodes.stream().anyMatch(n -> n.getName().equals(_snapshotName) &&

0 commit comments

Comments
 (0)