/
InstanceInfo.java
912 lines (817 loc) · 29.1 KB
/
InstanceInfo.java
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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
/*
* Copyright 2012 Netflix, Inc.
*
* 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
*
* http://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.netflix.appinfo;
import java.net.URL;
import java.util.LinkedHashSet;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.netflix.appinfo.AmazonInfo.MetaDataKey;
import com.netflix.appinfo.DataCenterInfo.Name;
import com.netflix.config.DynamicPropertyFactory;
import com.netflix.discovery.DiscoveryClient;
import com.netflix.discovery.converters.Auto;
import com.netflix.discovery.provider.Serializer;
import com.thoughtworks.xstream.annotations.XStreamAlias;
import com.thoughtworks.xstream.annotations.XStreamOmitField;
/**
* The class that holds information required for registration with
* <tt>Eureka Server</tt> and to be discovered by other components.
* <p>
* <code>@Auto</code> annotated fields are serialized as is; Other fields are
* serialized as specified by the <code>@Serializer</code>.
* </p>
*
* @author Karthik Ranganathan, Greg Kim
*
*/
@Serializer("com.netflix.discovery.converters.EntityBodyConverter")
@XStreamAlias("instance")
public class InstanceInfo {
private static final Logger logger = LoggerFactory
.getLogger(InstanceInfo.class);
private static final Pattern VIP_ATTRIBUTES_PATTERN = Pattern
.compile("\\$\\{(.*?)\\}");
public final static int DEFAULT_PORT = 7001;
public final static int DEFAULT_SECURE_PORT = 7002;
public final static int DEFAULT_COUNTRY_ID = 1; // US
private volatile String appName;
private volatile String ipAddr;
private volatile String sid = "na";
private volatile int port = DEFAULT_PORT;
private volatile int securePort = DEFAULT_SECURE_PORT;
@Auto
private volatile String homePageUrl;
@Auto
private volatile String statusPageUrl;
@Auto
private volatile String healthCheckUrl;
@Auto
private volatile String secureHealthCheckUrl;
@Auto
private volatile String vipAddress;
@Auto
private volatile String secureVipAddress;
@XStreamOmitField
private String statusPageRelativeUrl;
@XStreamOmitField
private String statusPageExplicitUrl;
@XStreamOmitField
private String healthCheckRelativeUrl;
@XStreamOmitField
private String healthCheckSecureExplicitUrl;
@XStreamOmitField
private String vipAddressUnresolved;
@XStreamOmitField
private String secureVipAddressUnresolved;
@XStreamOmitField
private String healthCheckExplicitUrl;
@Deprecated
private volatile int countryId = DEFAULT_COUNTRY_ID; // Defaults to US
private volatile boolean isSecurePortEnabled = false;
private volatile boolean isUnsecurePortEnabled = true;
private volatile DataCenterInfo dataCenterInfo;
private volatile String hostName;
private volatile InstanceStatus status = InstanceStatus.UP;
private volatile InstanceStatus overriddenstatus = InstanceStatus.UNKNOWN;
@XStreamOmitField
private volatile boolean isInstanceInfoDirty = false;
private volatile LeaseInfo leaseInfo;
@Auto
private volatile Boolean isCoordinatingDiscoveryServer = Boolean.FALSE;
@XStreamAlias("metadata")
private volatile Map<String, String> metadata = new ConcurrentHashMap<String, String>();
@Auto
private volatile Long lastUpdatedTimestamp = System.currentTimeMillis();
@Auto
private volatile Long lastDirtyTimestamp = System.currentTimeMillis();
@Auto
private volatile ActionType actionType;
@Auto
private volatile String asgName;
private String version = "unknown";
private InstanceInfo() {
}
public enum InstanceStatus {
UP, // Ready to receive traffic
DOWN, // Do not send traffic- healthcheck callback failed
STARTING, // Just about starting- initializations to be done - do not
// send traffic
OUT_OF_SERVICE, // Intentionally shutdown for traffic
UNKNOWN;
public static InstanceStatus toEnum(String s) {
for (InstanceStatus e : InstanceStatus.values()) {
if (e.name().equalsIgnoreCase(s)) {
return e;
}
}
return UNKNOWN;
}
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((getId() == null) ? 0 : getId().hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
InstanceInfo other = (InstanceInfo) obj;
if (getId() == null) {
if (other.getId() != null)
return false;
} else if (!getId().equals(other.getId()))
return false;
return true;
}
public enum PortType {
SECURE, UNSECURE
}
public static final class Builder {
private static final String HOSTNAME_INTERPOLATION_EXPRESSION = "${netflix.appinfo.hostname}";
private static final String COLON = ":";
private static final String HTTPS_PROTOCOL = "https://";
private static final String HTTP_PROTOCOL = "http://";
@XStreamOmitField
private InstanceInfo result;
private Builder() {
result = new InstanceInfo();
}
public Builder(InstanceInfo instanceInfo) {
result = instanceInfo;
}
public static Builder newBuilder() {
return new Builder();
}
/**
* Set the application name of the instance.This is mostly used in
* querying of instances.
*
* @param appName
* the application name.
* @return the instance info builder.
*/
public Builder setAppName(String appName) {
result.appName = appName;
if (result.appName != null) {
result.appName = result.appName.toUpperCase();
}
return this;
}
/**
* Sets the fully qualified hostname of this running instance.This is
* mostly used in constructing the {@link URL} for communicating with
* the instance.
*/
public Builder setHostName(String hostName) {
String existingHostName = result.hostName;
result.hostName = hostName;
if ((existingHostName != null) && !(hostName.equals(existingHostName))) {
refreshStatusPageUrl().refreshHealthCheckUrl()
.refreshVIPAddress().refreshSecureVIPAddress();
}
return this;
}
/**
* Sets the status of the instances.If the status is UP, that is when
* the instance is ready to service requests.
*/
public Builder setStatus(InstanceStatus status) {
result.status = status;
return this;
}
/**
* Sets the status overridden by some other external process.This is
* mostly used in putting an instance out of service to block traffic to
* it.
*/
public Builder setOverriddenStatus(InstanceStatus status) {
result.overriddenstatus = status;
return this;
}
/**
* Sets the ip address of this running instance.
*/
public Builder setIPAddr(String ip) {
result.ipAddr = ip;
return this;
}
/**
* Sets the identity of this application instance.
*/
@Deprecated
public Builder setSID(String sid) {
result.sid = sid;
return this;
}
/**
* Sets the port number that is used to service requests.
*/
public Builder setPort(int port) {
result.port = port;
return this;
}
/**
* Sets the secured port that is used to service requests.
*/
public Builder setSecurePort(int port) {
result.securePort = port;
return this;
}
/**
* Enabled or disable secure/non-secure ports .
*
* @param type
* Secure or Non-Secure.
* @param isEnabled
* true if enabled.
* @return the instance builder.
*/
public Builder enablePort(PortType type, boolean isEnabled) {
if (type == PortType.SECURE) {
result.isSecurePortEnabled = isEnabled;
} else {
result.isUnsecurePortEnabled = isEnabled;
}
return this;
}
@Deprecated
public Builder setCountryId(int id) {
result.countryId = id;
return this;
}
/**
* Sets the absolute home page {@link URL} for this instance. The users
* can provide the <code>homePageUrlPath</code> if the home page resides
* in the same instance talking to discovery, else in the cases where
* the instance is a proxy for some other server, it can provide the
* full {@link URL}. If the full {@link URL} is provided it takes
* precedence.
*
* <p>
* The full {@link URL} should follow the format
* http://${netflix.appinfo.hostname}:7001/ where the value
* ${netflix.appinfo.hostname} is replaced at runtime.
* </p>
*
* @param homePageUrlPath
* - The {@link URL} path for home page for this instance
* @param explicitUrl
* - The full {@link URL} for the home page
* @return the instance builder.
*/
public Builder setHomePageUrl(String relativeUrl, String explicitUrl) {
if (explicitUrl != null) {
result.homePageUrl = explicitUrl.replace(
HOSTNAME_INTERPOLATION_EXPRESSION, result.hostName);
} else if (relativeUrl != null) {
result.homePageUrl = HTTP_PROTOCOL + result.hostName + COLON
+ result.port + relativeUrl;
}
return this;
}
/**
* Sets the absolute status page {@link URL} for this instance. The
* users can provide the <code>statusPageUrlPath</code> if the status
* page resides in the same instance talking to discovery, else in the
* cases where the instance is a proxy for some other server, it can
* provide the full {@link URL}. If the full {@link URL} is provided it
* takes precedence.
*
* <p>
* The full {@link URL} should follow the format
* http://${netflix.appinfo.hostname}:7001/Status where the value
* ${netflix.appinfo.hostname} is replaced at runtime.
* </p>
*
* @param relativeUrl
* - The {@link URL} path for status page for this instance
* @param explicitUrl
* - The full {@link URL} for the status page
* @return - Builder instance
*/
public Builder setStatusPageUrl(String relativeUrl, String explicitUrl) {
result.statusPageRelativeUrl = relativeUrl;
result.statusPageExplicitUrl = explicitUrl;
if (explicitUrl != null) {
result.statusPageUrl = explicitUrl.replace(
HOSTNAME_INTERPOLATION_EXPRESSION, result.hostName);
} else if (relativeUrl != null) {
result.statusPageUrl = HTTP_PROTOCOL + result.hostName + COLON
+ result.port + relativeUrl;
}
return this;
}
/**
* Sets the absolute health check {@link URL} for this instance for both
* secure and non-secure communication The users can provide the
* <code>healthCheckUrlPath</code> if the healthcheck page resides in
* the same instance talking to discovery, else in the cases where the
* instance is a proxy for some other server, it can provide the full
* {@link URL}. If the full {@link URL} is provided it takes precedence.
*
* <p>
* The full {@link URL} should follow the format
* http://${netflix.appinfo.hostname}:7001/healthcheck where the value
* ${netflix.appinfo.hostname} is replaced at runtime.
* </p>
*
* @param relativeUrl
* - The {@link URL} path for healthcheck page for this
* instance.
* @param explicitUrl
* - The full {@link URL} for the healthcheck page.
* @param securehealthCheckUrl
* - The full {@link URL} for the secure healthcheck page.
* @return the instance builder
*/
public Builder setHealthCheckUrls(String relativeUrl,
String explicitUrl, String secureExplicitUrl) {
result.healthCheckRelativeUrl = relativeUrl;
result.healthCheckExplicitUrl = explicitUrl;
result.healthCheckSecureExplicitUrl = secureExplicitUrl;
if (explicitUrl != null) {
result.healthCheckUrl = explicitUrl.replace(
HOSTNAME_INTERPOLATION_EXPRESSION, result.hostName);
} else if (result.isUnsecurePortEnabled) {
result.healthCheckUrl = HTTP_PROTOCOL + result.hostName + COLON
+ result.port + relativeUrl;
}
if (secureExplicitUrl != null) {
result.secureHealthCheckUrl = secureExplicitUrl.replace(
HOSTNAME_INTERPOLATION_EXPRESSION, result.hostName);
} else if (result.isSecurePortEnabled) {
result.secureHealthCheckUrl = HTTPS_PROTOCOL + result.hostName
+ COLON + result.securePort + relativeUrl;
}
return this;
}
/**
* Sets the Virtual Internet Protocol address for this instance. The
* address should follow the format <code><hostname:port></code> This
* address needs to be resolved into a real address for communicating
* with this instance.
*
* @param vipAddress
* - The Virtual Internet Protocol address of this instance.
* @return the instance builder.
*/
public Builder setVIPAddress(String vipAddress) {
result.vipAddressUnresolved = vipAddress;
result.vipAddress = resolveDeploymentContextBasedVipAddresses(vipAddress);
return this;
}
/**
* Sets the Secure Virtual Internet Protocol address for this instance.
* The address should follow the format <hostname:port> This address
* needs to be resolved into a real address for communicating with this
* instance.
*
* @param secureVipAddress
* - The Virtual Internet Protocol address of this instance
* @return - Builder instance
*/
public Builder setSecureVIPAddress(String secureVIPAddress) {
result.secureVipAddressUnresolved = secureVIPAddress;
result.secureVipAddress = resolveDeploymentContextBasedVipAddresses(secureVIPAddress);
return this;
}
/**
* Sets the datacenter information.
*/
public Builder setDataCenterInfo(DataCenterInfo datacenter) {
result.dataCenterInfo = datacenter;
return this;
}
/**
* Set the instance lease information.
*
* @param info
* the lease information for this instance.
*/
public void setLeaseInfo(LeaseInfo info) {
result.leaseInfo = info;
}
/**
* Add arbitrary metadata to running instance
*/
public Builder add(String key, String val) {
result.metadata.put(key, val);
return this;
}
/**
* Replace the existing metadata map with a new one.
*
* @param mt
* the new metadata name.
* @return instance info builder.
*/
public Builder setMetadata(Map<String, String> mt) {
result.metadata = mt;
return this;
}
/**
* Returns the encapsulated instance info even it it is not built fully.
*
* @return the existing information about the instance.
*/
public InstanceInfo getRawInstance() {
return result;
}
/**
* Build the {@link InstanceInfo} object.
*/
public InstanceInfo build() {
if (!isInitialized()) {
throw new IllegalStateException("name is required!");
}
return result;
}
public boolean isInitialized() {
return (result.appName != null);
}
/**
* Sets the AWS ASG name for this instance.
*
* @param asgName
* the asg name for this instance.
* @return the instance info builder.
*/
public Builder setASGName(String asgName) {
result.asgName = asgName;
return this;
}
private Builder refreshStatusPageUrl() {
setStatusPageUrl(result.statusPageRelativeUrl,
result.statusPageExplicitUrl);
return this;
}
private Builder refreshHealthCheckUrl() {
setHealthCheckUrls(result.healthCheckRelativeUrl,
result.healthCheckExplicitUrl,
result.healthCheckSecureExplicitUrl);
return this;
}
private Builder refreshSecureVIPAddress() {
setSecureVIPAddress(result.secureVipAddressUnresolved);
return this;
}
private Builder refreshVIPAddress() {
setVIPAddress(result.vipAddressUnresolved);
return this;
}
}
/**
* Return the name of the application registering with discovery.
*/
public String getAppName() {
return appName;
}
/**
* Returns the fully qualified hostname of this running instance
* @return the hostname.
*/
public String getHostName() {
return hostName;
}
@Deprecated
public void setSID(String sid) {
this.sid = sid;
setIsDirty(true);
}
@Deprecated
public String getSID() {
return sid;
}
/**
*
* Returns the unique id of the instance.
* @return the unique id.
*/
public String getId() {
if (dataCenterInfo.getName() == Name.Amazon) {
return ((AmazonInfo) dataCenterInfo).get(MetaDataKey.instanceId);
} else {
return hostName;
}
}
/**
* Returns the ip address of the instance.
* @return - the ip address, in AWS scenario it is a private IP.
*/
public String getIPAddr() {
return ipAddr;
}
/**
*
* Returns the port number that is used for servicing requests.
* @return - the non-secure port number.
*/
public int getPort() {
return port;
}
/**
* Returns the status of the instance.
* @return the status indicating whether the instance can handle requests.
*/
public InstanceStatus getStatus() {
return status;
}
/**
* Returns the overridden status if any of the instance.
* @return the status indicating whether an external process has changed the status.
*/
public InstanceStatus getOverriddenStatus() {
return overriddenstatus;
}
/**
* Returns data center information identifying if it is AWS or not.
* @return the data center information.
*/
public DataCenterInfo getDataCenterInfo() {
return dataCenterInfo;
}
/**
* Returns the lease information regarding when it expires.
* @return the lease information of this instance.
*/
public LeaseInfo getLeaseInfo() {
return leaseInfo;
}
/**
* Sets the lease information regarding when it expires.
* @param info the lease information of this instance.
*/
public void setLeaseInfo(LeaseInfo info) {
leaseInfo = info;
}
/**
* Returns all application specific metadata set on the instance.
* @return application specific metadata.
*/
public Map<String, String> getMetadata() {
return metadata;
}
@Deprecated
public int getCountryId() {
return countryId;
}
/**
* Returns the secure port that is used for servicing requests.
* @return the secure port.
*/
public int getSecurePort() {
return securePort;
}
/**
* Checks whether a port is enabled for traffic or not.
* @param type indicates whether it is secure or non-secure port.
* @return true if the port is enabled, false otherwise.
*/
public boolean isPortEnabled(PortType type) {
if (type == PortType.SECURE) {
return isSecurePortEnabled;
} else {
return isUnsecurePortEnabled;
}
}
/**
* Returns the time elapsed since epoch since the instance status has been last updated.
* @return the time elapsed since epoch since the instance has been last updated.
*/
public long getLastUpdatedTimestamp() {
return lastUpdatedTimestamp;
}
/**
* Set the update time for this instance when the status was update.
*/
public void setLastUpdatedTimestamp() {
this.lastUpdatedTimestamp = System.currentTimeMillis();
}
/**
* Gets the home page {@link URL} set for this instance.
* @return home page {@link URL}
*/
public String getHomePageUrl() {
return homePageUrl;
}
/**
* Gets the status page {@link URL} set for this instance.
* @return status page {@link URL}
*/
public String getStatusPageUrl() {
return statusPageUrl;
}
/**
* Gets the absolute URLs for the health check page for both secure and
* non-secure protocols. If the port is not enabled then the URL is
* excluded.
*
* @return A Set containing the string representation of health check urls
* for secure and non secure protocols
*/
public Set<String> getHealthCheckUrls() {
Set<String> healthCheckUrlSet = new LinkedHashSet<String>();
if (this.isUnsecurePortEnabled) {
healthCheckUrlSet.add(healthCheckUrl);
}
if (this.isSecurePortEnabled) {
healthCheckUrlSet.add(secureHealthCheckUrl);
}
return healthCheckUrlSet;
}
/**
* Gets the Virtual Internet Protocol address for this instance. Defaults to
* hostname if not specified.
*
* @return - The Virtual Internet Protocol address
*/
public String getVIPAddress() {
return vipAddress;
}
/**
* Get the Secure Virtual Internet Protocol address for this instance.
* Defaults to hostname if not specified.
*
* @return - The Secure Virtual Internet Protocol address.
*/
public String getSecureVipAddress() {
return secureVipAddress;
}
/**
* Gets the last time stamp when this instance was touched.
* @return last timestamp when this instance was touched.
*/
public Long getLastDirtyTimestamp() {
return lastDirtyTimestamp;
}
/**
* Set the time indicating that the instance was touched.
* @param lastDirtyTimestamp time when the instance was touched.
*/
public void setLastDirtyTimestamp(Long lastDirtyTimestamp) {
this.lastDirtyTimestamp = lastDirtyTimestamp;
}
/**
* Set the status for this instance.
* @param status status for this instance.
*/
public synchronized void setStatus(InstanceStatus status) {
if (this.status != status) {
this.status = status;
setIsDirty(true);
}
}
/**
* Set the status for this instance without updating the dirty timestamp.
* @param status status for this instance.
*/
public synchronized void setStatusWithoutDirty(InstanceStatus status) {
if (this.status != status) {
this.status = status;
}
}
/**
* Sets the overridden status for this instance.Normally set by an external process
* to disable instance from taking traffic.
* @param status overridden status for this instance.
*/
public synchronized void setOverriddenStatus(InstanceStatus status) {
if (this.overriddenstatus != status) {
this.overriddenstatus = status;
}
}
/**
* Returns whether any state changed so that {@link DiscoveryClient} can
* check whether to retransmit info or not on the next heartbeat.
*/
public boolean isDirty() {
return isInstanceInfoDirty;
}
/**
* Sets the dirty flag so that the instance information can be carried to the
* discovery server on the next heartbeat.
* @param b - true if dirty, false otherwise.
*/
public void setIsDirty(boolean b) {
isInstanceInfoDirty = b;
this.lastDirtyTimestamp = System.currentTimeMillis();
}
/**
* Sets a flag if this instance is the same as the discovery server that is
* return the instances. This flag is used by the discovery clients to
* identity the discovery server which is coordinating/returning the
* information.
*/
public void setIsCoordinatingDiscoveryServer() {
String instanceId = getId();
if ((instanceId != null)
&& (instanceId.equals(ApplicationInfoManager.getInstance()
.getInfo().getId()))) {
isCoordinatingDiscoveryServer = Boolean.TRUE;
} else {
isCoordinatingDiscoveryServer = Boolean.FALSE;
}
}
/**
* Finds if this instance is the coordinating discovery server.
*
* @return - true, if this instance is the coordinating discovery server,
* false otherwise.
*/
public Boolean isCoordinatingDiscoveryServer() {
return isCoordinatingDiscoveryServer;
}
/**
* Returns the type of action done on the instance in the server.Primarily used for
* updating deltas in the {@DiscoveryClient} instance.
* @return action type done on the instance.
*/
public ActionType getActionType() {
return actionType;
}
/**
* Set the action type performed on this instance in the server.
* @param actionType action type done on the instance.
*/
public void setActionType(ActionType actionType) {
this.actionType = actionType;
}
/**
* Get AWS autoscaling group name if any.
* @return autoscaling group name of this instance.
*/
public String getASGName() {
return this.asgName;
}
/**
* Returns the specification version of this application
*/
@Deprecated
public String getVersion() {
return version;
}
public enum ActionType {
ADDED, // Added in the discovery server
MODIFIED, // Changed in the discovery server
DELETED //Deleted from the discovery server
}
/**
* Register application specific metadata to be sent to the discovery server.
* @param runtimeMetadata Map containing key/value pairs.
*/
synchronized void registerRuntimeMetadata(
Map<String, String> runtimeMetadata) {
metadata.putAll(runtimeMetadata);
setIsDirty(true);
}
/**
* Convert <code>VIPAddress</code> by substituting environment variables if necessary.
* @param vipAddressMacro the macro for which the interpolation needs to be made.
* @return a string representing the final <code>VIPAddress</code> after substitution.
*/
private static String resolveDeploymentContextBasedVipAddresses(
String vipAddressMacro) {
String result = vipAddressMacro;
if (vipAddressMacro == null) {
return null;
}
Matcher matcher = VIP_ATTRIBUTES_PATTERN.matcher(result);
while (matcher.find()) {
String key = matcher.group(1);
String value = DynamicPropertyFactory.getInstance()
.getStringProperty(key, null).get();
logger.debug("att:" + matcher.group());
logger.debug(", att key:" + key);
logger.debug(", att value:" + value);
logger.debug("");
result = result.replaceAll("\\$\\{" + key + "\\}", value);
matcher = VIP_ATTRIBUTES_PATTERN.matcher(result);
}
return result;
}
}