20
20
21
21
import org .apache .logging .log4j .LogManager ;
22
22
import org .apache .logging .log4j .Logger ;
23
- import org .apache .lucene .util .CloseableThreadLocal ;
24
23
import org .elasticsearch .action .support .ContextPreservingActionListener ;
25
24
import org .elasticsearch .client .OriginSettingClient ;
26
25
import org .elasticsearch .common .io .stream .StreamInput ;
31
30
import org .elasticsearch .common .settings .Settings ;
32
31
import org .elasticsearch .http .HttpTransportSettings ;
33
32
34
- import java .io .Closeable ;
35
33
import java .io .IOException ;
36
34
import java .nio .charset .StandardCharsets ;
37
35
import java .util .Collections ;
41
39
import java .util .List ;
42
40
import java .util .Map ;
43
41
import java .util .Set ;
44
- import java .util .concurrent .atomic .AtomicBoolean ;
45
42
import java .util .function .BiConsumer ;
46
43
import java .util .function .BinaryOperator ;
47
44
import java .util .function .Function ;
81
78
* </pre>
82
79
*
83
80
*/
84
- public final class ThreadContext implements Closeable , Writeable {
81
+ public final class ThreadContext implements Writeable {
85
82
86
83
public static final String PREFIX = "request.headers" ;
87
84
public static final Setting <Settings > DEFAULT_HEADERS_SETTING = Setting .groupSetting (PREFIX + "." , Property .NodeScope );
@@ -94,7 +91,7 @@ public final class ThreadContext implements Closeable, Writeable {
94
91
private static final Logger logger = LogManager .getLogger (ThreadContext .class );
95
92
private static final ThreadContextStruct DEFAULT_CONTEXT = new ThreadContextStruct ();
96
93
private final Map <String , String > defaultHeader ;
97
- private final ContextThreadLocal threadLocal ;
94
+ private final ThreadLocal < ThreadContextStruct > threadLocal ;
98
95
private final int maxWarningHeaderCount ;
99
96
private final long maxWarningHeaderSize ;
100
97
@@ -113,34 +110,23 @@ public ThreadContext(Settings settings) {
113
110
}
114
111
this .defaultHeader = Collections .unmodifiableMap (defaultHeader );
115
112
}
116
- threadLocal = new ContextThreadLocal ( );
113
+ threadLocal = ThreadLocal . withInitial (() -> DEFAULT_CONTEXT );
117
114
this .maxWarningHeaderCount = SETTING_HTTP_MAX_WARNING_HEADER_COUNT .get (settings );
118
115
this .maxWarningHeaderSize = SETTING_HTTP_MAX_WARNING_HEADER_SIZE .get (settings ).getBytes ();
119
116
}
120
117
121
- @ Override
122
- public void close () {
123
- threadLocal .close ();
124
- }
125
-
126
118
/**
127
119
* Removes the current context and resets a default context. The removed context can be
128
120
* restored by closing the returned {@link StoredContext}.
129
121
*/
130
122
public StoredContext stashContext () {
131
123
final ThreadContextStruct context = threadLocal .get ();
132
- threadLocal .set (null );
124
+ threadLocal .set (DEFAULT_CONTEXT );
133
125
return () -> {
134
126
// If the node and thus the threadLocal get closed while this task
135
127
// is still executing, we don't want this runnable to fail with an
136
128
// uncaught exception
137
- try {
138
- threadLocal .set (context );
139
- } catch (IllegalStateException e ) {
140
- if (isClosed () == false ) {
141
- throw e ;
142
- }
143
- }
129
+ threadLocal .set (context );
144
130
};
145
131
}
146
132
@@ -399,13 +385,6 @@ public boolean isSystemContext() {
399
385
return threadLocal .get ().isSystemContext ;
400
386
}
401
387
402
- /**
403
- * Returns <code>true</code> if the context is closed, otherwise <code>true</code>
404
- */
405
- boolean isClosed () {
406
- return threadLocal .closed .get ();
407
- }
408
-
409
388
@ FunctionalInterface
410
389
public interface StoredContext extends AutoCloseable {
411
390
@ Override
@@ -617,55 +596,6 @@ private void writeTo(StreamOutput out, Map<String, String> defaultHeaders) throw
617
596
}
618
597
}
619
598
620
- private static class ContextThreadLocal extends CloseableThreadLocal <ThreadContextStruct > {
621
- private final AtomicBoolean closed = new AtomicBoolean (false );
622
-
623
- @ Override
624
- public void set (ThreadContextStruct object ) {
625
- try {
626
- if (object == DEFAULT_CONTEXT ) {
627
- super .set (null );
628
- } else {
629
- super .set (object );
630
- }
631
- } catch (NullPointerException ex ) {
632
- /* This is odd but CloseableThreadLocal throws a NPE if it was closed but still accessed.
633
- to get a real exception we call ensureOpen() to tell the user we are already closed.*/
634
- ensureOpen ();
635
- throw ex ;
636
- }
637
- }
638
-
639
- @ Override
640
- public ThreadContextStruct get () {
641
- try {
642
- ThreadContextStruct threadContextStruct = super .get ();
643
- if (threadContextStruct != null ) {
644
- return threadContextStruct ;
645
- }
646
- return DEFAULT_CONTEXT ;
647
- } catch (NullPointerException ex ) {
648
- /* This is odd but CloseableThreadLocal throws a NPE if it was closed but still accessed.
649
- to get a real exception we call ensureOpen() to tell the user we are already closed.*/
650
- ensureOpen ();
651
- throw ex ;
652
- }
653
- }
654
-
655
- private void ensureOpen () {
656
- if (closed .get ()) {
657
- throw new IllegalStateException ("threadcontext is already closed" );
658
- }
659
- }
660
-
661
- @ Override
662
- public void close () {
663
- if (closed .compareAndSet (false , true )) {
664
- super .close ();
665
- }
666
- }
667
- }
668
-
669
599
/**
670
600
* Wraps a Runnable to preserve the thread context.
671
601
*/
@@ -680,19 +610,9 @@ private ContextPreservingRunnable(Runnable in) {
680
610
681
611
@ Override
682
612
public void run () {
683
- boolean whileRunning = false ;
684
613
try (ThreadContext .StoredContext ignore = stashContext ()){
685
614
ctx .restore ();
686
- whileRunning = true ;
687
615
in .run ();
688
- whileRunning = false ;
689
- } catch (IllegalStateException ex ) {
690
- if (whileRunning || threadLocal .closed .get () == false ) {
691
- throw ex ;
692
- }
693
- // if we hit an ISE here we have been shutting down
694
- // this comes from the threadcontext and barfs if
695
- // our threadpool has been shutting down
696
616
}
697
617
}
698
618
@@ -749,21 +669,9 @@ public void onRejection(Exception e) {
749
669
750
670
@ Override
751
671
protected void doRun () throws Exception {
752
- boolean whileRunning = false ;
753
672
threadsOriginalContext = stashContext ();
754
- try {
755
- creatorsContext .restore ();
756
- whileRunning = true ;
757
- in .doRun ();
758
- whileRunning = false ;
759
- } catch (IllegalStateException ex ) {
760
- if (whileRunning || threadLocal .closed .get () == false ) {
761
- throw ex ;
762
- }
763
- // if we hit an ISE here we have been shutting down
764
- // this comes from the threadcontext and barfs if
765
- // our threadpool has been shutting down
766
- }
673
+ creatorsContext .restore ();
674
+ in .doRun ();
767
675
}
768
676
769
677
@ Override
0 commit comments